Python schedule任务调度及其使用方式
scheduler 类常用的结构要领如下:
scheduler(timefunc=time.monotonic, delayfunc=time.sleep) 可以向该结构要领中传入 2 个参数(虽然也可以不提供,由于都有默认值),别离暗示的寄义如下:timefunc:指定天生时刻戳的函数,默认行使 time.monotonic 来天生时刻戳; delayfunc:在未达到指按时刻前,通过该参数可以指定阻塞使命执行的函数,默认回收 time.sleep() 函数来阻塞措施。 其它,scheduler 类中还提供有一些要领,表 1 摆列了常用的一些。 表 1 scheduler 类常用要领 要领名目 成果 scheduler.enter(delay, priority, action, argument=(), kwargs={}) 在 time 划定的时刻后,执行 action 参数指定的函数,个中 argument 和 kwargs 认真为 action 指定的函数传参,priority 参数执行要执利用命的品级,当统一时刻点有多个使命必要执行时,品级越高( priority 值越小)的使命会优先执行。该函数会返回一个 event,可用来打消该使命。 scheduler.cancel(event) 打消 event 使命。留意,假如 event 参数执行的使命不存在,则会激发 ValueError 错误。 scheduler.run(blocking=True) 运行全部必要调治的使命。假如挪用该要领的 blocking 参数为 True,该要领将会阻塞线程,直到全部被调治的使命都执行完成。 下面措施树模了 scheduler 类的用法。
import threading from sched import scheduler
def action(arg): print(arg)
#界说线程要挪用的要领,*add可吸取多个以非要害字方法传入的参数 def thread_action(*add): #建设使命调治工具 sche = scheduler() #界说优先级 i = 3 for arc in add: # 指定1秒后执行action函数 sche.enter(1, i, action,argument=(arc,)) i = i - 1 #执行全部调治的使命 sche.run()
#界说为线程要领传入的参数 my_tuple = ("", "", "") #建设线程 thread = threading.Thread(target = thread_action,args =my_tuple) (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |