参考文章
https://apscheduler.readthedocs.io/en/latest/modules/triggers/cron.html#module-apscheduler.triggers.cron
https://www.cnblogs.com/gdjlc/p/11432526.html
定时任务模块简单使用,
由于任务所有的执行都通过定时任务
因此我感觉只用BlockingScheduler就可以了
#-*- coding:utf-8 -*-
from apscheduler.schedulers.blocking import BlockingScheduler
import tzlocal
import time
import logging
logging.basicConfig(filename='test.log', filemode='a')
def my_job(text):
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print('{} --- {}'.format(text, t))
time.sleep(15)
#print('Hello World!')
def my_job2(text):
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print('{} --- {}'.format(text, t))
time.sleep(5)
def main():
sched = BlockingScheduler(timezone=str(tzlocal.get_localzone()))
# 采用interval固定间隔模式,每隔五秒只执行一次
sched.add_job(my_job, 'interval', seconds=5, args=['job1'])
sched.add_job(my_job2, 'interval', seconds=5, args=['job2'])
sched._logger = logging
sched.start()
if __name__ == '__main__':
main()
网友评论