python调度框架APScheduler使用的实例详解

本篇文章主要介绍了详解python调度框架apscheduler使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

最近在研究python调度框架APScheduler使用的路上,那么今天也算个学习笔记吧!

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BackgroundScheduler()  scheduler.add_job(tick, 'interval', seconds=3)  #间隔3秒钟执行一次  scheduler.start()  #这里的调度任务是独立的一个线程  print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    # This is here to simulate application activity (which keeps the main thread alive).    while True:      time.sleep(2)  #其他任务是独立的线程执行      print('sleep!')  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

非阻塞调度,在指定的时间执行一次

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BackgroundScheduler()  #scheduler.add_job(tick, 'interval', seconds=3)  scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')  #在指定的时间,只执行一次  scheduler.start()  #这里的调度任务是独立的一个线程  print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    # This is here to simulate application activity (which keeps the main thread alive).    while True:      time.sleep(2)  #其他任务是独立的线程执行      print('sleep!')  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

非阻塞的方式,采用cron的方式执行

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport timeimport osfrom apscheduler.schedulers.background import BackgroundSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BackgroundScheduler()  #scheduler.add_job(tick, 'interval', seconds=3)  #scheduler.add_job(tick, 'date', run_date='2016-02-14 15:01:05')  scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')  '''    year (int|str) – 4-digit year    month (int|str) – month (1-12)    day (int|str) – day of the (1-31)    week (int|str) – ISO week (1-53)    day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)    hour (int|str) – hour (0-23)    minute (int|str) – minute (0-59)    second (int|str) – second (0-59)        start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)    end_date (datetime|str) – latest possible date/time to trigger on (inclusive)    timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)      *  any  Fire on every value    */a  any  Fire every a values, starting from the minimum    a-b  any  Fire on any value within the a-b range (a must be smaller than b)    a-b/c  any  Fire every c values within the a-b range    xth y  day  Fire on the x -th occurrence of weekday y within the month    last x  day  Fire on the last occurrence of weekday x within the month    last  day  Fire on the last day within the month    x,y,z  any  Fire on any matching expression; can combine any number of any of the above expressions  '''  scheduler.start()  #这里的调度任务是独立的一个线程  print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    # This is here to simulate application activity (which keeps the main thread alive).    while True:      time.sleep(2)  #其他任务是独立的线程执行      print('sleep!')  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

阻塞的方式,间隔3秒执行一次

立即学习“Python免费学习笔记(深入)”;

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport osfrom apscheduler.schedulers.blocking import BlockingSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BlockingScheduler()  scheduler.add_job(tick, 'interval', seconds=3)    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    scheduler.start()  #采用的是阻塞的方式,只有一个线程专职做调度的任务  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

采用阻塞的方法,只执行一次

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport osfrom apscheduler.schedulers.blocking import BlockingSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BlockingScheduler()  scheduler.add_job(tick, 'date', run_date='2016-02-14 15:23:05')    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    scheduler.start()  #采用的是阻塞的方式,只有一个线程专职做调度的任务  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

采用阻塞的方式,使用cron的调度方法

# coding=utf-8"""Demonstrates how to use the background scheduler to schedule a job that executes on 3 secondintervals."""from datetime import datetimeimport osfrom apscheduler.schedulers.blocking import BlockingSchedulerdef tick():  print('Tick! The time is: %s' % datetime.now())if __name__ == '__main__':  scheduler = BlockingScheduler()  scheduler.add_job(tick, 'cron', day_of_week='6', second='*/5')  '''    year (int|str) – 4-digit year    month (int|str) – month (1-12)    day (int|str) – day of the (1-31)    week (int|str) – ISO week (1-53)    day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)    hour (int|str) – hour (0-23)    minute (int|str) – minute (0-59)    second (int|str) – second (0-59)        start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)    end_date (datetime|str) – latest possible date/time to trigger on (inclusive)    timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)      *  any  Fire on every value    */a  any  Fire every a values, starting from the minimum    a-b  any  Fire on any value within the a-b range (a must be smaller than b)    a-b/c  any  Fire every c values within the a-b range    xth y  day  Fire on the x -th occurrence of weekday y within the month    last x  day  Fire on the last occurrence of weekday x within the month    last  day  Fire on the last day within the month    x,y,z  any  Fire on any matching expression; can combine any number of any of the above expressions  '''    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))  try:    scheduler.start()  #采用的是阻塞的方式,只有一个线程专职做调度的任务  except (KeyboardInterrupt, SystemExit):    # Not strictly necessary if daemonic mode is enabled but should be done if possible    scheduler.shutdown()    print('Exit The Job!')

登录后复制

以上就是python调度框架APScheduler使用的实例详解的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2273536.html

(0)
上一篇 2025年2月27日 13:28:33
下一篇 2025年2月26日 01:11:59

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 关于Python中多线程的详解

    这篇文章主要介绍了python 多线程实例详解的相关资料,需要的朋友可以参考下 Python 多线程实例详解 多线程通常是新开一个后台线程去处理比较耗时的操作,Python做后台线程处理也是很简单的,今天从官方文档中找到了一个Demo. 实…

    编程技术 2025年2月27日
    200
  • 学习Python到底能干什么

    python是一种计算机程序设计语言,又被称为胶水语言,可以用混合编译的方式使用c/c++/java等语言的库。你可能已经听说过很多种流行的编程语言,比如在大学里感觉非常难学的c语言,进入社会非常流行的java语言,以及适合初学者的basi…

    编程技术 2025年2月27日
    200
  • Python笔试题(2017最新)Python面试题笔试题

    想找一份Python开发工作吗?那你很可能得证明自己知道如何使用Python。下面这些问题涉及了与Python相关的许多技能,问题的关注点主要是语言本身,不是某个特定的包或模块。每一个问题都可以扩充为一个教程,如果可能的话。某些问题甚至会涉…

    2025年2月27日
    200
  • 分享一个用python遍历字符串(含汉字)的方法

    这篇文章主要介绍了python 遍历字符串(含汉字)实例详解的相关资料,需要的朋友可以参考下 python 遍历字符串(含汉字)实例详解 s = “中国china”for j in s:  print j 登录后复制 首先一个,你这个&#8…

    编程技术 2025年2月27日
    200
  • 实例详解python模拟登录并且保持cookie的方法

    模拟登录相信对大家来说都不陌生,下面这篇文章主要给大家介绍了关于python模拟登录并且保持cookie的方法,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。 前言 最近在爬行 nosec.org 的数据,看了…

    编程技术 2025年2月27日
    200
  • 分享关于python容器的总结归纳

    这篇文章主要介绍了python 容器总结整理的相关资料,需要的朋友可以参考下 python 容器总结整理 list 可变数组 tuple 立即学习“Python免费学习笔记(深入)”; 不可变数组 dict 键值对(key-value)的字…

    编程技术 2025年2月27日
    200
  • 介绍Python读取指定目录下指定后缀文件并保存为docx的方法

    这篇文章主要介绍了python读取指定目录下指定后缀文件并保存为docx,需要的朋友可以参考下 最近有个奇葩要求 要项目中的N行代码 申请专利啥的 然后作为程序员当然不能复制粘贴 用代码解决。。 使用python-docx读写docx文件 …

    编程技术 2025年2月27日
    200
  • 详细讲解python中的关键字“with”与上下文管理器

    这篇文章主要介绍了关于python中关键字”with”和上下文管理器的相关资料,文中介绍的非常详细,相信对大家学习或者使用python具有一定的参考价值,需要的朋友们下面来一起看看吧。 前言 如果你有阅读源码的习惯,…

    编程技术 2025年2月27日
    200
  • python中关于编码转换的实例详解

    在日常渗透,漏洞挖掘,甚至是ctf比赛中会遇到各种编码,常常伴随着这些编码之间的各种转换。下面这篇文章主要介绍了python中编码转换妙用的相关资料,需要的朋友们可以参考借鉴,下面来一起看看吧。 前言 记得刚入门那个时候,自己处理编码转换问…

    2025年2月27日
    200
  • 关于Python中Tuple和Dict详细解析

    这篇文章主要介绍了关于python中元祖(tuple)和字典(dict)的相关资料,文中通过示例代码介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。 前言 本文记录了对于Python的数据类型中元祖(Tuple)…

    编程技术 2025年2月27日
    200

发表回复

登录后才能评论