使用APScheduler3.0.1 实现定时任务的方法

需求是在某一指定的时刻执行操作

网上的建议多为通过调用Scheduler的add_date_job实现

不过APScheduler 3.0.1与之前差异较大, 无法通过上述方法实现

参考 https://apscheduler.readthedocs.org/en/latest/userguide.html APScheduler 3.0.1的userguide 解决问题

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.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)
 except (KeyboardInterrupt, SystemExit):
  scheduler.shutdown() # Not strictly necessary if daemonic mode is enabled but should be done if possible

实例的代码实现每3秒执行一次tick方法,虽然与需求不符,但发现add_interval_job在APScheduler 3.0.1中 已经被

scheduler.add_job(tick, 'interval', seconds=3)

取代。

help(scheduler.add_job)得到

add_job(func, trigger=None, args=None, kwargs=None, id=None, name=None, misfire_grace_time=undefined, coalesce=undefined, max_instances=undefined, next_run_time=undefined, jobstore='default', executor='default', replace_existing=False, **trigger_args)
Adds the given job to the job list and wakes up the scheduler if it's already running.

Any option that defaults to undefined will be replaced with the corresponding default value when the job is scheduled (which happens when the scheduler is started, or immediately if the scheduler is already running).

The func argument can be given either as a callable object or a textual reference in the package.module:some.object format, where the first half (separated by :) is an importable module and the second half is a reference to the callable object, relative to the module.

The trigger argument can either be:
the alias name of the trigger (e.g. date, interval or cron), in which case any extra keyword arguments to this method are passed on to the trigger's constructor
an instance of a trigger class

由此可知,第参数为trigger,可取值为 date、interval、cron, **trigger_args为该trigger的构造函数。

通过源码找到DateTrigger 的构造函数

def __init__(self, run_date=None, timezone=None)

所以,只需将指定的时间传入add_job

scheduler.add_job(tick, 'date', run_date='2014-11-11 14:48:00')

以上这篇使用APScheduler3.0.1 实现定时任务的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python3实现定时任务的四种方式

    最近做一个小程序开发任务,主要负责后台部分开发:根据项目需求,需要实现三个定时任务: 1>定时更新微信token,需要2小时更新一次: 2>商品定时上线: 3>定时检测后台服务是否存活: 使用Python去实现这三个任务,这里需要使用定时相关知识点: Python实现定点与定时任务方式比较多,找到下面四中实现方式,每个方式都有自己应用场景:下面来快速介绍Python中常用的定时任务实现方式: 1>循环+sleep: 2>线程模块中Timer类: 3>schedule模块

  • django使用django-apscheduler 实现定时任务的例子

    下载: pip install apscheduler pip install django-apscheduler 将 django-apscheduler 加到项目中settings的INSTALLED_APPS中 INSTALLED_APPS = [ .... 'django_apscheduler', ] 然后迁移文件后 ./manage.py migrate 生成两个表:django_apscheduler_djangojob 和 django_apscheduler_djangojo

  • Python中定时任务框架APScheduler的快速入门指南

    前言 大家应该都知道在编程语言中,定时任务是常用的一种调度形式,在Python中也涌现了非常多的调度模块,本文将简要介绍APScheduler的基本使用方法. 一.APScheduler介绍 APScheduler是基于Quartz的一个python定时任务框架,实现了Quartz的所有功能,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务. APScheduler提供了多种不同的调度器,方便开发者根据自己的实际需要进行使用:同时也提供了不同的存储机

  • 详解Python 定时框架 Apscheduler原理及安装过程

    在我们的日常工作自动化测试当中,几乎超过一半的功能都需要利用定时的任务来推动触发,例如在我们项目中有一个定时监控模块,根据自己设置的频率定时跑测试用例,定时检测是否存在线上紧急任务等等,这些都涉及到了有关定时任务的问题,很多情况下,大多数人会选择window的任务计划程序,但如果程序不在window平台下运行,就不能定时启动了:当然也可利用time模块的time.sleep()方法使程序休眠来达到定时任务的目的,但定时任务多了,代码可能看起来不太那么友好且有很大的局限性,因此,此时的 Apsch

  • 详解django中使用定时任务的方法

    今天介绍在django中使用定时任务的两种方式. 方式一: APScheduler 1)安装: pip install apscheduler 2)使用: from apscheduler.scheduler import Scheduler from django.core.cache import cache # 实例化 sched = Scheduler() # 每30秒执行一次 @sched.interval_schedule(seconds=30) def sched_test():

  • 详解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

  • 使用APScheduler3.0.1 实现定时任务的方法

    需求是在某一指定的时刻执行操作 网上的建议多为通过调用Scheduler的add_date_job实现 不过APScheduler 3.0.1与之前差异较大, 无法通过上述方法实现 参考 https://apscheduler.readthedocs.org/en/latest/userguide.html APScheduler 3.0.1的userguide 解决问题 from datetime import datetime import time import os from apsch

  • Yii框架创建cronjob定时任务的方法分析

    本文实例讲述了Yii框架创建cronjob定时任务的方法.分享给大家供大家参考,具体如下: 1. 添加环境配置 protected/config/console.php <?php require_once('env.php'); // This is the configuration for yiic console application. // Any writable CConsoleApplication properties can be configured here. retu

  • Linux crontab定时任务配置方法(详解)

    CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在每天的非高峰负荷时间段运行作业,或在一周或一月中的不同时段运行.cron是系统主要的调度进程,可以在无需人工干预的情况下运行作业.crontab命令允许用户提交.编辑或删除相应的作业.每一个用户都可以有一个crontab文件来保存调度信息.系统管理员可以通过cron.deny 和 cron

  • yii2 commands模式以及配置crontab定时任务的方法

    一 ,检测环境: 首先我们切换到项目根目录,yii2正常安装的话有一个commands文件夹,里面有一个示例文件HelloController.php <?php namespace app\commands; use yii\console\Controller; class HelloController extends Controller { public function actionIndex($message = 'hello world') { echo $message . &qu

  • 在Java Web项目中添加定时任务的方法

    在Java Web程序中加入定时任务,这里介绍两种方式:1.使用监听器注入:2.使用Spring注解@Scheduled注入. 推荐使用第二种形式. 一.使用监听器注入 ①:创建监听器类: import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class TimerDataTaskListener implements ServletContextListener

  • Java Web实现添加定时任务的方法示例

    本文实例讲述了Java Web实现添加定时任务的方法.分享给大家供大家参考,具体如下: 定时任务时间控制类 /** * 定时任务时间控制 * * @author liming * */ public class TimerManager { // 时间间隔 private static final long PERIOD_DAY = 24 * 60 * 60 * 1000; public TimerManager() { Calendar calendar = Calendar.getInsta

  • spring boot整合quartz实现多个定时任务的方法

    最近收到了很多封邮件,都是想知道spring boot整合quartz如何实现多个定时任务的,由于本人生产上并没有使用到多个定时任务,这里给个实现的思路. 1.新建两个定时任务,如下: public class ScheduledJob implements Job{ @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("sched

  • Linux Crontab Shell脚本实现秒级定时任务的方法

    一.编写Shell脚本crontab.sh #!/bin/bash step=1 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do $(php '/home/www/php/crontab/crontab.php') sleep $step done exit 0 二.crontab -e 输入以下语句,然后:wq 保存退出 # m h dom mon dow command * * * * * /home/www/php/crontab

  • Linux部署python爬虫脚本,并设置定时任务的方法

    去年因项目需要,用python写了个爬虫.因爬到的数据需要存到生产环境的PG数据库.所以需要将脚本部署到CentOS服务器,并设置定时任务,自动启动脚本. 实施步骤如下: 1.安装pip(操作系统自带了python2.6可以直接用,但是没有pip) # 下载pip安装包 wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --

  • mysql实现设置定时任务的方法分析

    本文实例讲述了mysql实现设置定时任务的方法.分享给大家供大家参考,具体如下: 今天遇到了个需要每天定时执行的任务,在mysql数据库里面提供了这样的功能,正好整理下分享出来. 1.首先检查是否开启了定时任务 查看event是否开启 : SHOW VARIABLES LIKE '%event_sche%'; 将事件计划开启 : SET GLOBAL event_scheduler = 1; 将事件计划关闭 : SET GLOBAL event_scheduler = 0; 关闭事件任务 : A

随机推荐