本文介绍了Celery 4.0中的定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,由于Celery 3.1装饰器 @periodic_task 被废弃.

As I know, since Celery 3.1 decorator @periodic_task is depricated.

因此,我尝试从celery docs 运行一个示例并无法意识到我在做什么错.

So I am trying to run an example from celery docs, and can't realise, what am I doing wrong.

我在 task_planner.py 中有以下代码:

from celery import Celery
from kombu import Queue, Exchange



class Config(object):
    CELERY_QUEUES = (
        Queue(
            'try',
            exchange=Exchange('try'),
            routing_key='try',
        ),
    )
celery = Celery('tasks',
                backend='redis://',
                broker='redis://localhost:6379/0')
celery.config_from_object(Config)


celery.conf.beat_schedule = {
    'planner': {
        'task': 'some_task',
        'schedule': 5.0,
    },
}


@celery.task(queue='try')
def some_task():
    print('Hooray')

当我运行: celery -A task_planner worker -l info -B 时,我仅收到以下信息: [2016-11-27 19:06:56,119:INFO/Beat]计划程序:每5秒钟发送一次适当的任务计划程序(some_task).

And when I run: celery -A task_planner worker -l info -B, I recieve only the following: [2016-11-27 19:06:56,119: INFO/Beat] Scheduler: Sending due task planner (some_task) every 5 sec.

但是我期望输出为'Hooray'.

But I am expecting the output 'Hooray'.

那么,我想念什么?

推荐答案

已找到解决方案.我有任务:

Have found the solution.I had the task:

@celery.task(queue='try')
def some_task():
    print('Hooray')

我打印了它的名字:

print(some_task)

有以下几点:

<@task: task_planner.some_task of tasks:0x7fceaaf5b9e8>

所以我只是在这里将任务的名称从 some_task 更改为 task_planner.some_task :

So I just changed the name of the task from some_task to task_planner.some_task here:

celery.conf.beat_schedule = {
    'planner': {
        'task': 'task_planner.some_task',
        'schedule': 5.0,
    },
}

它奏效了!

[2016-11-29 10:09:57,697: WARNING/PoolWorker-3] Hooray

注意.您应运行带有worker的beat(如果任务与beat在同一模块中)和日志级别"info"以查看结果:

Note. You should run beat with worker (if task in the same module as beat) and loglevel 'info' in order to see the results:

celery -A task_planner worker -B -l info

这篇关于Celery 4.0中的定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:55