Python定时任务模块——schedule

import schedule
import time
 
def show(name):
    print("%s is showing" % name)
 
name = 'wohucanglong'
# 每隔十分钟执行一次
schedule.every(10).minutes.do(show, name)
# 每隔一小时执行一次
schedule.every().hour.do(show, name)
# 每天的09:00执行一次
schedule.every().day.at("09:00").do(show, name)
# 每隔一个月执行一次
schedule.every().monday.do(show, name)
# 每周一13:00执行一次任务
schedule.every().monday.at("13:00").do(show, name)
 
while True:
	# run_pending:运行所有被schedule调用的任务
    schedule.run_pending()
    time.sleep(1)

time.sleep(1)是必须要加上的,因为while True是死循环,CPU占用率极高
原因稍作解释:多进程的调度虽然是根据时间片进行轮转的,但若一个进程如果是死循环, 当切换到别的进程时,别的进程告诉系统自己没什么事情要做,不需要那么多的时间,这个时候系统就会切换到下一个进程,直到回到这个死循环的进程上,而这个进程无论什么时候都再循环,所以一直会报告有事情要做,系统就会把尽可能多的时间分给他。

附上一个schedule的官方文档 https://schedule.readthedocs.io/en/stable/

猜你喜欢

转载自blog.csdn.net/qq_36355119/article/details/82905571