asyncio实现定时任务
示例代码
import os
import asyncio
from datetime import datetime
from apscheduler.schedulers.asyncio import AsyncIOScheduler
def tick():
print('Tick! The time is: %s' % datetime.now())
if __name__ == '__main__':
scheduler = AsyncIOScheduler()
scheduler.add_job(tick, 'interval', seconds=3)
scheduler.add_job(
spider.start, 'cron', minute="*/5",
next_run_time=datetime.now(),
misfire_grace_time=30
)
scheduler.add_job(
baidu_zhishu,
'cron',
args=(kws, ),
day_of_week='0',
hour="09",
minute="30",
misfire_grace_time=60,
next_run_time=datetime.now()
)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
# Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
try:
asyncio.get_event_loop().run_forever()
except (KeyboardInterrupt, SystemExit):
pass
apscheduler模块的其它示例
https://github.com/agronholm/apscheduler/tree/master/examples