django core task 介绍在django中定时任务的几种实现方式

  1. 1. 定时调度的问题
    1. 1.1. 工作原理
      1. 1.1.1. 启动一个rqworker default

定时调度的问题

1.前言

最近在项目中做一个定时统计数据的功能,如何做到定时调用统计方法呢?

在网上看了一下,大致有三种方式


1. [编写Django Command ,用cron进行定时调度](http://www.cnblogs.com/linjiqin/p/3965046.html)

2. 第一种方式的升级版实现[django-crontab实现Django定时任务](http://www.zhidaow.com/post/django-crontab)

3. 编写Django Command 然后rq_scheduler定时把任务提交给队列,由worker进行处理

这里我选用的是第三种方式

2.编写Django Command 然后rq_scheduler定时把任务提交给队列,由worker进行处理

工作原理

*Putting a job in the scheduler

*Running a scheduler that will move scheduled jobs into queues when the time comes

*RQ Scheduler comes with a script rqscheduler that runs a scheduler process that polls Redis once every minute and move scheduled jobs to the relevant queues when they need to be executed:

scheduler.schedule(
scheduled_time=datetime.now(), # Time for first execution, in UTC timezone
func=func, # Function to be queued
args=[arg1, arg2], # Arguments passed into function when executed
kwargs={'foo': 'bar'}, # Keyword arguments passed into function when executed
interval=60, # Time before the function is called again, in seconds
repeat=10 # Repeat this number of times (None means repeat forever)
)

##使用方法

###初始化:

In [1]: from analytics.etl import analytics_etl
In [2]: analytics_etl.init()
  1. 安装rq_scheduler
    pip install rq_scheduler
  2. 编写测试command scheduler_command.py
class Command(BaseCommand):

help = "process analytics data"

def handle(self, *args, **options):
first_process_datetime=datetime.now()+timedelta(days=1)
first_process_datetime.replace(hour=1,minute=0,second=0,microsecond=0)
first_process_datetime=dateformat.local_to_utc(first_process_datetime)
scheduler.schedule(first_process_datetime,process,interval=interval)

###调用job

python manage.py scheduler_command --settings=local_settings

###启动rqscheduler

rqscheduler执行一次就OK
python manage.py rqscheduler --settings=local_settings

启动一个rqworker default

启动一个工作者,
python manage.py rqworker default --settings=local_settings

###查看当前有多少job:

import django_rq

scheduler=django_rq.get_scheduler('default')

jobs=scheduler.get_jobs()

###取消job

scheduler.cancel(job)