periodic tasks in Celery 4.0

谁都会走 提交于 2019-12-23 04:30:14

问题


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

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

I have the following code in 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')

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.

But I am expecting the output 'Hooray'.

So, what am I missing?


回答1:


Have found the solution. I had the task:

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

I printed it's name:

print(some_task)

Got the following:

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

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,
    },
}

And it worked!

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

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


来源:https://stackoverflow.com/questions/40832819/periodic-tasks-in-celery-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!