Reputation: 35
I am using the schedule module in python to make a scheduler, the scheduler works, but once it runs my other python programs hang/get stuck..
I am using tkinter to make a UI for the scheduler and schedule module to schedule tasks..
here is the code for my program..
def add_account(name):
with open(f"---{name}.txt",mode="w")as file:
file.close()
def write_data(account_name,data):
with open(f"---{account_name}.txt",mode="a") as file:
file.write(data + "\n")
file.close()
def set_remainder(name , data , time2):
def thing(data, name):
messagebox.showinfo(f"Reminder Alert for user : {name}", f"The remainder for user: {name} is now!! Remainder:{thing}:")
return schedule.CancelJob()
schedule.every(time2).seconds.do(thing, data, time)
while True:
schedule.run_pending()
time.sleep(1)
def open_account(name):
with open(f"---{name}.txt",mode="r") as opened_account:
remainders = opened_account.read()
opened_account.close()
win2 = Tk()
win2.geometry("100x100")
remainders = Label(win2, text=remainders)
remainders.pack()
new_remainder_label = Label(win2, text="remainder:")
new_remainder_label.pack()
new_remainder_entry = Entry(win2)
new_remainder_entry.pack()
time_label = Label(win2, text="time:")
time_label.pack()
time_entry = Entry(win2)
time_entry.pack()
submit_button = Button(win2, text="add reminder", command=lambda:[ write_data(name,new_remainder_entry.get()), set_remainder(name, new_remainder_entry.get(), float(time_entry.get()) ) ])
submit_button.pack()
win2.mainloop()
def main():
main_win = Tk()
main_win.geometry("100x100")
account_name_entry = Entry(main_win)
account_regiter_button = Button(main_win, text="add account", command=lambda:add_account(account_name_entry.get()))
account_open_button = Button(main_win, text="open account", command=lambda:open_account(account_name_entry.get()))
account_regiter_button.pack()
account_open_button.pack()
account_name_entry.pack()
main_win.mainloop()
thanks in advance..
--CodeMaster.
Upvotes: 1
Views: 3764
Reputation: 100
This behaviour is correct with schedule
library because once you execute while
loop, it will loop around that task define with schedule
library for eternity.
The best way to avoid this is by using python Threading
.
I have worked with Qt and Tkinter GUI toolkit and I always prefer Threading
to avoid the deadlock.
Upvotes: 1
Reputation: 942
This is how you use the schedule module to run several jobs parallel.
import threading
import time
import schedule
def job():
print("I'm running on thread %s" % threading.current_thread())
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
while 1:
schedule.run_pending()
time.sleep(1)
Upvotes: 3