热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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

本篇文章主要介绍了详解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 
本文来源gaodai.ma#com搞##代!^码7网
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秒执行一次

# 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!')

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



推荐阅读
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • 本文介绍了使用Python编写购物程序的实现步骤和代码示例。程序启动后,用户需要输入工资,并打印商品列表。用户可以根据商品编号选择购买商品,程序会检测余额是否充足,如果充足则直接扣款,否则提醒用户。用户可以随时退出程序,在退出时打印已购买商品的数量和余额。附带了完整的代码示例。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 怎么在PHP项目中实现一个HTTP断点续传功能发布时间:2021-01-1916:26:06来源:亿速云阅读:96作者:Le ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 这篇文章主要介绍了Python拼接字符串的七种方式,包括使用%、format()、join()、f-string等方法。每种方法都有其特点和限制,通过本文的介绍可以帮助读者更好地理解和运用字符串拼接的技巧。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
author-avatar
mark0003
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有