求助。这个定时任务怎么用呀
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler(timezone='Asia/Shanghai')
@scheduler.scheduled_job('cron', id='my_job', hour=18, minute=30)
def func1():
print('is a test')
if __name__ == '__main__':
func1()
我想每天下午6点30调用 func1() 这样写不行么? if __name__ == '__main__':
func1()
scheduler.start() # 1. 任务没有开始
# 2. BlockingScheduler会阻塞主线程的运行,而BackgroundScheduler不会阻塞, 所以要手动处理主线程什么时候退出
while True:
time.sleep(1)
from apscheduler.schedulers.background import BlockingScheduler
scheduler = BlockingScheduler(timezone='Asia/Shanghai')
#
# def func1():
# print('is a test')
@scheduler.scheduled_job('cron', id='my_job', hour=10, minute=17)
def func1():
print('is a test')
if __name__ == '__main__':
# scheduler.add_job(func1, 'cron', hour=9, minute=11)
try:
scheduler.start()
func1()
except Exception as a:
pass
明白了。谢谢
页:
[1]