import schedule
import time
def job(work):
print(work)
schedule.every().day.at("10:30").do(job(work))
while True:
schedule.run_pending()
time.sleep(1)
How to call job(work) inside my do() function. if i give job() it works fine, but if i give job(work) it throwing error. what to do with this? any help
This is in the schedule FAQ:
import schedule
import time
def job(work):
print(work)
schedule.every().day.at("10:30").do(job,work="") # make "" whatever string you want
while True:
schedule.run_pending()
time.sleep(1)
Related
I want to be able to be able to run a function at a random time as long as the program is running.
Let's say I have this function:
def sample_func:
print("Function is running.")
and some other code running. As long as the program is being run, I want this function to run at a random amount of time between 5 to 10 minutes while other code is being run. Is this possible?
Here's the code you're looking. Set the sleep timer for 5-10 minutes.
Insert your main flow code inside the "main flow" function.
import random
import threading
import time
def random_function():
print("Function is running.")
def call_random_function():
while True:
time.sleep(random.randint(1, 5))
random_function()
def main_flow():
"""
Do your main flow here
"""
a = 1
while a < 10:
print(a)
a += 1
time.sleep(10)
if __name__ == '__main__':
threading.Thread(target=call_random_function).start()
main_flow()
print("Program is finished.")
I wrote this script a few days ago in VSCode. It's supposed to open my zoom meetings every day at a certain time and I really want it to work because it's one of my first projects. I think I wrote everything correctly, however. it won't work. Do you see what I'm missing?
import webbrowser
import schedule
import time
url_deutsch = "https://us04web.zoom.us/j/79379252675?pwd=cm1NUUlSMlg4aSsyWXVwL3UybzhOUT09"
url_chemie = 'https://zoom.us/j/95643744370?pwd=clRkMDNEVTZ3dzErZWUvZWZFVFVvQT09'
url_englisch = 'https://us02web.zoom.us/j/3470923893?pwd=ejdIaVdaL21yWXVPaU9wdnBDalhYUT09'
url_opv = 'https://us04web.zoom.us/j/79683686777?pwd=QXhmVTZFZitCbUowZTh1bDFTWjRVQT09'
url_test = 'google.com'
chrome_path = 'C:/Program Files/Google/Chrome/Application/chrome.exe %s'
#Zoom aleman
def open_link_deutsch():
webbrowser.get(chrome_path).open(url_deutsch)
return
#Zoom quimica
def open_link_chemie():
webbrowser.get(chrome_path).open(url_chemie)
return
#Zoom ingles
def open_link_englisch():
webbrowser.get(chrome_path).open(url_englisch)
return
def open_link_test():
webbrowser.get(chrome_path).open(url_test)
return
while True:
schedule.run_pending()
time.sleep(0)
#Schedule
schedule.every().monday.at('13:30').do(open_link_deutsch)
schedule.every().tuesday.at('11:00').do(open_link_deutsch)
schedule.every().wednesday.at('09:10').do(open_link_deutsch)
schedule.every().wednesday.at('07:15').do(open_link_chemie)
schedule.every().tuesday.at('07:15').do(open_link_englisch)
schedule.every().friday.at('09:10').do(open_link_englisch)
schedule.every().friday.at('12:51').do(open_link_test)
After you run this code, it will not wait for the events that you sheduled.
According to official schedule documentation on PyPI, you should add
import time
to imports and something like
while True:
schedule.run_pending()
time.sleep(1)
at the end of your code. This will execute forever and check if any event is pending for this exact moment every second.
You need to set the schedule before entering the while-loop. The code is doing something, it's checking to see if any jobs are scheduled, constantly. Move the schedule.every() calls before the loop, and choose a more reasonable time to wait between schedule polls. The code below polls about every 60s which I think is fine, but you could make it poll more frequently.
import schedule
import time
import webbrowser
#Zoom quimica
def open_link_chemie():
webbrowser.get(chrome_path).open(url_chemie)
return
#Zoom ingles
def open_link_englisch():
webbrowser.get(chrome_path).open(url_englisch)
return
def open_link_test():
webbrowser.get(chrome_path).open(url_test)
return
#Schedule
schedule.every().monday.at('13:30').do(open_link_deutsch)
schedule.every().tuesday.at('11:00').do(open_link_deutsch)
schedule.every().wednesday.at('09:10').do(open_link_deutsch)
schedule.every().wednesday.at('07:15').do(open_link_chemie)
schedule.every().tuesday.at('07:15').do(open_link_englisch)
schedule.every().friday.at('09:10').do(open_link_englisch)
schedule.every().friday.at('12:51').do(open_link_test)
while True:
schedule.run_pending()
time.sleep(60)
I need to schedule a python script which can exit and kill it self at a given time. For scheduling, I am using python schedule and below is the code:
import schedule
from threading import Thread
import time
import sys
def exit_data():
print("Exiting")
sys.exit()
def exit_data_thread():
schedule.every().day.at('13:20').do(exit_data)
while True:
schedule.run_pending()
time.sleep(1)
def main():
Thread(target=exit_data_thread).start()
while True:
time.sleep(1)
main()
Function exit_data() runs at given time and it prints Exiting but do not exit. It only prints Exiting and then it keeps running. I have also used quit instead of sys.exit(). Please help. Thanks
Try to send signal to yourself :p
import schedule
from threading import Thread
import time
import sys
import os
import signal
def exit_data():
print("Exiting")
# sys.exit()
os.kill(os.getpid(), signal.SIGTERM)
def exit_data_thread():
schedule.every(3).seconds.do(exit_data)
while True:
schedule.run_pending()
time.sleep(1)
def main():
Thread(target=exit_data_thread).start()
while True:
time.sleep(1)
main()
To close the entire program within a thread, you can use os._exit(). Calling sys.exit() will only exit the thread, not the entire program.
I used the schedule library to schedule a function every X seconds:
Want I want is to run this function on separate thread. I found this in the documentation on how to Run the scheduler in a separate thread but I didn't understand what he did.
Is there someone who can explain to me how to do that ?
Update:
This what I tried:
def post_to_db_in_new_thread():
schedule.every(15).seconds.do(save_db)
t1 = threading.Thread(target=post_to_db_in_new_thread, args=[])
t1.start()
You don't really need to update schedule in every task
import threading
import time
import schedule
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
schedule.every(15).seconds.do(run_threaded, save_db)
while 1:
schedule.run_pending()
time.sleep(1)
I've been trying for around half an hour now and just cant seem to wrap my head around what im doing wrong here.
Working Code:
import threading
from time import sleep
def printX():
threading.Timer(5.0, printX).start()
print("five")
printX()
while True:
print("1")
sleep(1)
This works, however I need to be able to dynamically assign what the print statement will be along with the delay.
Desired Code:
import threading
from time import sleep
def printX(time, message):
threading.Timer(int(time), printX).start()
print(str(message)
printX(time, message)
while True:
print("Rest of the program continues")
sleep(1)
Thanks for any help in advance :).
threading.Timer could pass arguments with args:
threading.Timer(int(time), printX, (time, message)).start()
read more on its doc.
An alternative method is to define a class with printX as an inner function.
class thread:
def __init__(self, time, message):
self.time = time
self.message = message
def printX(self):
threading.Timer(int(self.time), self.printX).start()
print(str(self.message))
thread(3,"test message").printX()
while True:
print("Rest of the program continues")
sleep(1)