# _*_ coding: utf-8 _*_"""调度的使用"""import timeimport datetimefrom threading import Timerfrom apscheduler.schedulers.blocking import BlockingSchedulerfrom apscheduler.schedulers.background import BackgroundSchedulerdef print_hello():print("TimeNow in func: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))returnif __name__ == "__main__":# 1. 使用Threading模块中的Timer# t = Timer(2, print_hello)## print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# t.start()# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# exit()# 2. BlockingScheduler:在进程中运行单个任务,调度器是唯一运行的东西, 采用阻塞的方式scheduler = BlockingScheduler()# 采用固定时间间隔(interval)的方式,每隔5秒钟执行一次scheduler.add_job(print_hello, "interval", seconds=5)# 采用date的方式,在特定时间只执行一次# scheduler.add_job(print_hello, "date", run_date=datetime.datetime.now() + datetime.timedelta(seconds=5))# print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# scheduler.start()# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# exit()# 3. BackgroundScheduler: 适合于要求任何在程序后台运行的情况,当希望调度器在应用后台执行时使用。采用非阻塞的方式scheduler = BackgroundScheduler()# 采用固定时间间隔(interval)的方式,每隔3秒钟执行一次scheduler.add_job(print_hello, "interval", seconds=5)# 这是一个独立的线程# print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# scheduler.start()# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))# while True:# time.sleep(2)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。