Celery Tasks Disappear - 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

after a few seconds I do the following:

for result in results:
    print result.result

prints this:

(values ​​are not returned in what looks like a template)

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

The second installation of Windows OS + installation:

everything works as expected, still not sure what happened here ...


Tasks are also accidentally missing from the Django admin interface ...


Does anyone know what is going on ?: |

+3
source share
1 answer

The .delay () task is asynchronous. By the way, all AMQP's job is to make tasks asynchronous. If you want synchronized behavior, what's 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)

( ) , .

task.delay celery.result.AsyncResult. AsyncResult.result AsyncResult.state == .

:

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..."

() , :

for result in results:
    print result.get()

TaskSets, , .

, AJAX .

+4

Source: https://habr.com/ru/post/1782363/


All Articles