Django Celery - How to start a task with a delay of n - seconds - countdown flag is ignored

▼魔方 西西 提交于 2019-12-12 14:11:21

问题


In my Django project I'm running some asynchronous tasks using Celery (docs), Django-Celery and RabbitMQ as the broker. Whereas it works in general, I have two problems with my setup:

a) the task execution seems to be joined with my request thread. Thus the user http request seems to wait until the task has been executed

b) the task execution seems to ignore the countdown flag

For testing purposes I have setup a simple TestTask:

from celery.task import Task
from celery.registry import tasks

#in project_management.tasks.py
class TestTask(Task):
    def run(self, x, y):
        print "running TestTask"
        return x + y

tasks.register(TestTask)

Running this task from within the console gives me the following result:

python manage.py shell
from project_management.tasks import TestTask
result = TestTask.apply_async(args=[5, 5], kwargs={}, countdown=10)#immediately outputs "running TestTask"
result.result -> immediately returns 10
result.ready() -> immediately returns True

Thus the countdown flag set to 10 is totally ignored. Any idea what could be wrong with my setup?

I'm starting Celery and RabbitMQ with the following commands:

RABBITMQ_NODE_PORT=5672 rabbitmq-server
python manage.py celeryd --loglevel=info

Update:

I think this problem relates somehow to timezone settings. See this thread for more info. Anyhow not sure how to circumvent it. I executed this tests, always having the same result that the result is immediately available:

>>> from project_management.tasks import add
>>> from datetime import timedelta, datetime
>>> eta = datetime.now() + timedelta(seconds=60)
>>> result = add.apply_async(args=[5, 5], kwargs={}, eta=eta)
>>> result.ready()
True
>>> eta = datetime.utcnow() + timedelta(seconds=60)
>>> result = add.apply_async(args=[5, 5], kwargs={}, eta=eta)
>>> result.ready()
True

回答1:


For future reference. The task has been executed immediately because I have set the CELERY_ALWAYS_EAGER setting to True, which has the effect that tasks are run synchronously. Setting this variable to False solved my problem.

Hope this helps somebody.



来源:https://stackoverflow.com/questions/16498966/django-celery-how-to-start-a-task-with-a-delay-of-n-seconds-countdown-flag

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