Schedule
是用于周期性作业的进程内调度器,这些作业使用生成器模式进行配置。Schedule允许您使用简单、人性化的语法,以预先确定的时间间隔定期运行Python函数(或任何其他可调用函数)。
null
计划库用于在每天的特定时间或每周的特定日期安排任务。我们还可以设置24小时格式的时间,以确定任务应该何时运行。基本上,计划库会将系统时间与您设置的计划时间相匹配。一旦计划时间和系统时间匹配,就会调用作业功能(计划的命令功能)。
安装
$ pip install schedule
日程调度程序 班
-
schedule.every(interval=1)
: 调用默认调度程序实例上的每个。安排一个新的定期工作。 -
schedule.run_pending()
: 在默认调度程序实例上调用run_挂起。运行计划运行的所有作业。 -
schedule.run_all(delay_seconds=0)
: 在默认调度程序实例上调用run_all。运行所有作业,无论它们是否计划运行。 -
schedule.idle_seconds()
: 在默认调度程序实例上调用空闲时间。 -
schedule.next_run()
: 调用默认调度程序实例上运行的下一个_。下一个作业应该运行的日期时间。 -
schedule.cancel_job(job)
: 在默认计划程序实例上调用cancel_作业。删除计划的作业。
日程作业(间隔,调度程序=无) 班
调度程序使用的周期性作业。
参数:
间隔时间: 一定时间单位的量 调度程序: 此作业在作业中完全配置后将向其注册的计划程序实例。do()。
计划的基本方法。工作
-
at(time_str)
: 将工作安排在每天的特定时间。调用此命令仅对计划每N天运行一次的作业有效。参数: time_str–XX:YY格式的字符串。 返回: 调用的作业实例
-
do(job_func, *args, **kwargs)
: 指定每次运行作业时应调用的作业函数。当作业运行时,任何附加参数都会传递给job_func。参数: job_func–要计划的功能 返回: 调用的作业实例
-
run()
: 运行作业并立即重新安排。 返回: 作业函数返回的返回值 -
to(latest)
: 安排作业以不规则(随机)间隔运行。例如,每个(A)。至(B)。秒每N秒执行一次作业功能,使A<=N<=B。
让我们来看看实现
# Schedule Library imported import schedule import time # Functions setup def sudo_placement(): print ( "Get ready for Sudo Placement at Geeksforgeeks" ) def good_luck(): print ( "Good Luck for Test" ) def work(): print ( "Study and work hard" ) def bedtime(): print ( "It is bed time go rest" ) def geeks(): print ( "Shaurya says Geeksforgeeks" ) # Task scheduling # After every 10mins geeks() is called. schedule.every( 10 ).minutes.do(geeks) # After every hour geeks() is called. schedule.every().hour.do(geeks) # Every day at 12am or 00:00 time bedtime() is called. schedule.every().day.at( "00:00" ).do(bedtime) # After every 5 to 10mins in between run work() schedule.every( 5 ).to( 10 ).minutes.do(work) # Every monday good_luck() is called schedule.every().monday.do(good_luck) # Every tuesday at 18:00 sudo_placement() is called schedule.every().tuesday.at( "18:00" ).do(sudo_placement) # Loop so that the scheduling task # keeps on running all time. while True : # Checks whether a scheduled task # is pending to run or not schedule.run_pending() time.sleep( 1 ) |
参考: https://schedule.readthedocs.io/en/stable/api.html
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END