Python 调度程序工作时如何执行多个任务?

Python 调度程序工作时如何执行多个任务?,python,schedule,Python,Schedule,我正在使用python中的schedule模块制作一个调度器,调度器可以工作,但一旦它运行我的其他python程序,就会挂起/卡住 我正在使用tkinter为调度程序和调度模块创建一个UI来调度任务 这是我的程序代码 def add_账户(名称): 打开(f“-{name}.txt”,mode=“w”)作为文件: file.close()文件 def写入数据(账户名称、数据): 打开(f“-{account\u name}.txt”,mode=“a”)作为文件: file.write(数据+“\

我正在使用python中的schedule模块制作一个调度器,调度器可以工作,但一旦它运行我的其他python程序,就会挂起/卡住

我正在使用tkinter为调度程序和调度模块创建一个UI来调度任务

这是我的程序代码

def add_账户(名称):
打开(f“-{name}.txt”,mode=“w”)作为文件:
file.close()文件
def写入数据(账户名称、数据):
打开(f“-{account\u name}.txt”,mode=“a”)作为文件:
file.write(数据+“\n”)
file.close()文件
def set_余数(名称、数据、时间2):
def对象(数据、名称):
showinfo(f“用户{name}的提醒警报”,f“用户{name}的余数现在是!!余数:{thing}:)
返回计划。取消作业()
计划。每(时间2)。秒。做(事情、数据、时间)
尽管如此:
schedule.run_pending()
时间。睡眠(1)
def开立账户(名称):
以open(f“--{name}.txt”,mode=“r”)作为开户账户:
remainders=开立的账户。read()
开立账户。关闭()
win2=Tk()
win2.几何体(“100x100”)
remainders=Label(win2,text=remainders)
remainders.pack()
新的\u余数\u标签=标签(win2,text=“余数:)
新的\u剩余物\u标签.pack()
新建\剩余\条目=条目(win2)
新的\u剩余\u条目.pack()
时间\标签=标签(win2,text=“时间:)
时间标签包()
时间\输入=输入(win2)
time_entry.pack()
提交按钮=按钮(win2,text=“add rementer”,command=lambda:[写入数据(名称,新的剩余项,get()),设置剩余项(名称,新的剩余项,get(),float(时间项,get())])
提交按钮。包()
win2.mainloop()
def main():
main_win=Tk()
主要几何图形(“100x100”)
帐户名称输入=输入(主输入)
account\u regiter\u button=按钮(main\u win,text=“add account”,command=lambda:add\u account(account\u name\u entry.get())
account\u open\u button=按钮(main\u win,text=“open account”,command=lambda:open\u account(account\u name\u entry.get())
account\u regiter\u button.pack()
帐户\打开\按钮。包()
帐户\名称\条目.pack()
main_win.mainloop()
提前谢谢


--CodeMaster。

这是如何使用调度模块并行运行多个作业的

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)

这是如何使用调度模块并行运行多个作业的

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)

此行为在
计划
库中是正确的,因为一旦您在执行
循环时执行
,它将围绕该任务循环使用
计划
库定义

避免这种情况的最好方法是使用python
线程化。

我曾经使用过Qt和Tkinter GUI工具包,我总是喜欢
线程化
,以避免死锁。

这种行为在
调度
库中是正确的,因为一旦执行
while
循环,它将永远围绕着任务定义
调度
库循环

避免这种情况的最好方法是使用python
线程化。

我使用过Qt和Tkinter GUI工具包,我总是喜欢
线程化
,以避免死锁。

这样我可以在主线程上运行Tkinter,在另一个线程上运行调度?你不应该阻塞主线程,因为需要频繁调用“run_pending”。Tkinter可能会阻塞主回路。试着在另一个线程中运行Tkiter这是怎么回事?我对tkinter线程不太了解,或者根本不了解线程,所以我可以在主线程上运行tkinter,在另一个线程上运行调度?你不应该阻塞主线程,因为需要频繁调用“run_pending”。Tkinter可能会阻塞主回路。试着在另一个线程中运行Tkiter这是怎么回事?我对tkinter的线程不太了解,或者根本不了解线程