A
A
alega192019-08-26 18:13:39
Celery
alega19, 2019-08-26 18:13:39

How to set periodic tasks in Celery declaratively with a dictionary in app.conf = {...}?

It works:

# tasks.py:
import celery

app = celery.Celery('tasks', broker='redis://127.0.0.1:6379')

@app.task
def show(arg):
    print(arg)

app.add_periodic_task(5, show.s(42), name='task-name')

And this doesn't work:
# tasks.py:
import celery

app = celery.Celery('tasks', broker='redis://127.0.0.1:6379')

@app.task
def show(arg):
    print(arg)

app.conf.beat_schedule = {
    'task-name': {
        'task': 'show',
        'schedule': 5.0,
        'args': (42,),
    },
}
app.conf.timezone = 'UTC'

Worker crashes with the following error:
[2019-08-26 18:09:16,333: ERROR/MainProcess] Received unregistered task of type 'show'.
The message has been ignored and discarded.
Did you remember to import the module containing this task?
Or maybe you're using relative imports?
Please see
docs.celeryq.org/en/latest/internals/protocol.html
for more information.
The full contents of the message body was:
b'[[42], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (79b)
Traceback (most recent call last):
File "/home/oleg/projects/asm/.venv/lib/python3.6/site-packages/celery/worker/consumer/consumer.py", line 558,

I run it like this:
celery -A tasks beat -s ./celerybeat-schedule  # планировщик
celery -A tasks worker  # воркер

But I want to set tasks in the second way.
What's wrong?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
alega19, 2019-08-26
@alega19

Because it needs to be like this:

# tasks.py:
import celery

app = celery.Celery('tasks', broker='redis://127.0.0.1:6379')

@app.task
def show(arg):
    print(arg)

app.conf.beat_schedule = {
    'task-name': {
        'task': 'tasks.show',  # instead 'show'
        'schedule': 5.0,
        'args': (42,),
    },
}
app.conf.timezone = 'UTC'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question