简体   繁体   English

芹菜任务消失 - Django /芹菜

[英]Celery tasks vanishing - Django/Celery

here is my code: 这是我的代码:

my task 我的任务

from celery.decorators import task

@task()
def add(x, y):
    return x + y

using my tasks 使用我的任务

from core.tasks import add

results = []

for i in range(100):
    results.append(add.delay(i, i))

problem 1 问题1

after a few seconds I perform the following: 几秒钟后,我执行以下操作:

for result in results:
    print result.result

prints this: 打印这个:

(the values are not returned in what looks like a pattern) (这些值不会以模式的形式返回)

None
None
None
None
8
None
None
None
None
18
None
None
None
None
28
None
None
None
None
38
...

Second clean OS install+setup: 第二个干净的OS安装+设置:

everything is working as expected, still not sure what happened here... 一切都按预期工作,仍不确定这里发生了什么......


Tasks are also randomly missing within the Django admin interface... Django管理界面中的任务也随机丢失......


Anybody know what is going on? 谁知道发生了什么事? :| :|

The task.delay() is asynchronous. task.delay()是异步的。 BTW, the whole AMQP thing is about making tasks asynchronous. 顺便说一下,整个AMQP的事情就是让任务异步。 If you want synchronous behavior, what is the point of using celery? 如果你想要同步行为,那么使用芹菜有什么意义呢?

from celery.decorators import task

@task()
def add(x, y, results):
    results.append(x + y)

------------8<-------------
from core.tasks import add

results = []

for i in range(100):
    add.delay(i, i, results)

Wait a few seconds before printing (while the consumers do their work) and be aware that results may came up out of order. 打印前等待几秒钟(当消费者完成工作时),并注意结果可能会出现故障。

Method task.delay will return a celery.result.AsyncResult instance. 方法task.delay将返回celery.result.AsyncResult实例。 Before using AsyncResult.result you should check for AsyncResult.state == SUCCESS. 在使用AsyncResult.result之前,您应该检查AsyncResult.state == SUCCESS。

Your final print loop could be: 您的最终打印循环可能是:

for result in results:
    while not result.state.ready():
        time.sleep(secs)
    if result.state == u'SUCCESS':
        print result.result
    else:
        print "Something Rotten in the State of Denmark..."

This is (almost) the same as: 这(几乎)与以下相同:

for result in results:
    print result.get()

Look at TaskSets , they are better than storing results in a list. 查看TaskSet ,它们比将结果存储在列表中更好。

In practice people store results in the database and place the wait loop in the client that will then hammer the server via AJAX every few seconds until completion. 在实践中,人们将结果存储在数据库中,并将等待循环放在客户端中,然后每隔几秒就通过AJAX锤击服务器直到完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM