全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

详解python调度框架APScheduler使用

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

# coding=utf-8
"""
Demonstrates how to use the background scheduler to schedule a job that executes on 3 second
intervals.
"""

from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def 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 second
intervals.
"""

from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def 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 second
intervals.
"""

from datetime import datetime
import time
import os

from apscheduler.schedulers.background import BackgroundScheduler


def 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 second
intervals.
"""

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler


def 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 second
intervals.
"""

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler


def 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 second
intervals.
"""

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler


def 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  # python3  # Python定时库Apscheduler的简单使用  # Python中定时任务框架APScheduler的快速入门指南  # Python定时任务APScheduler的实例实例详解  # Python定时任务工具之APScheduler使用方式  # Python任务调度利器之APScheduler详解  # Python APScheduler执行使用方法详解  # Python定时库APScheduler的原理以及用法示例  # 的是  # 只有一个  # 也算  # 大家多多  # 学习笔记  # 路上  # Break  # exit  # format  # simulate  # application  # nt  # Ctrl  # interval  # add_job  # Press  # start  # seconds  # activity  # mode 


相关文章: 韩国代理服务器如何选?解析IP设置技巧与跨境访问优化指南  定制建站策划方案_专业建站与网站建设方案一站式指南  如何选择PHP开源工具快速搭建网站?  建站之星安装步骤有哪些常见问题?  如何在Windows 2008云服务器安全搭建网站?  昆明高端网站制作公司,昆明公租房申请网上登录入口?  保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?  c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】  太平洋网站制作公司,网络用语太平洋是什么意思?  建站主机如何选?高性价比方案全解析  太原网站制作公司有哪些,网约车营运证查询官网?  如何在阿里云完成域名注册与建站?  电商网站制作价格怎么算,网上拍卖流程以及规则?  宁波自助建站系统如何快速打造专业企业网站?  css网站制作参考文献有哪些,易聊怎么注册?  如何安全更换建站之星模板并保留数据?  如何通过PHP快速构建高效问答网站功能?  如何在建站宝盒中设置产品搜索功能?  建站之星3.0如何解决常见操作问题?  陕西网站制作公司有哪些,陕西凌云电器有限公司官网?  如何快速生成高效建站系统源代码?  如何快速生成专业多端适配建站电话?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  如何在腾讯云服务器上快速搭建个人网站?  官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站  如何通过服务器快速搭建网站?完整步骤解析  网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?  建站之星2.7模板:企业网站建设与h5定制设计专题  做企业网站制作流程,企业网站制作基本流程有哪些?  公众号网站制作网页,微信公众号怎么制作?  建站之星在线客服如何快速接入解答?  如何选择高效响应式自助建站源码系统?  免费的流程图制作网站有哪些,2025年教师初级职称申报网上流程?  如何在云主机快速搭建网站站点?  枣阳网站制作,阳新火车站打的到仙岛湖多少钱?  建站之星如何取消后台验证码生成?  济南网站制作的价格,历城一职专官方网站?  专业企业网站设计制作公司,如何理解商贸企业的统一配送和分销网络建设?  建站之星免费版是否永久可用?  公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  外贸公司网站制作哪家好,maersk船公司官网?  免费视频制作网站,更新又快又好的免费电影网站?  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?  ,在苏州找工作,上哪个网站比较好?  如何挑选最适合建站的高性能VPS主机?  网站制作免费,什么网站能看正片电影?  如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法  如何在香港免费服务器上快速搭建网站? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。