python3--APScheduler定时任务模块

参考文章
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()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容