2017-07-19 83 views
0

我必須使用Celery 4.0.2和RabbitMQ 3.6.10來處理異步任務。然後,我按照這個教程:https://www.codementor.io/uditagarwal/asynchronous-tasks-using-celery-with-django-du1087f5kDjango - Celery with RabbitMQ:任務總是保留在PENDING

但是,我的任務有一個小問題,因爲它不可能有結果。我的任務始終處於「待定」狀態。

我的問題是我得做什麼才能得到結果?

非常感謝您的回答。

這裏我的代碼:

這裏我__init_.py:

from __future__ import absolute_import, unicode_literals 

# This will make sure the app is always imported when 
# Django starts so that shared_task will use this app. 
from .celery import app as celery_app 

__all__ = ['celery_app'] 

這裏我setting.py的一部分:

BROKER_URL = 'amqp://guest:[email protected]//' 
CELERY_ACCEPT_CONTENT = ['json'] 
CELERY_TASK_SERIALIZER = 'json' 
CELERY_RESULT_SERIALIZER = 'json' 

這裏我celery.py:

from __future__ import absolute_import, unicode_literals 
import os 
from celery import Celery 

# set the default Django settings module for the 'celery' program. 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') 

app = Celery('mysite', 
    backend='amqp', 
    broker='amqp://[email protected]//') 

# Using a string here means the worker don't have to serialize 
# the configuration object to child processes. 
# - namespace='CELERY' means all celery-related configuration keys 
# should have a `CELERY_` prefix. 
app.config_from_object('django.conf:settings', namespace='CELERY') 

# Load task modules from all registered Django app configs. 
app.autodiscover_tasks() 


@app.task(bind=True) 
def debug_task(self): 
    print('Request: {0!r}'.format(self.request)) 

而且我tasks.py

# Create your tasks here 
from __future__ import absolute_import, unicode_literals 
from celery import shared_task 


@shared_task 
def add(x, y): 
    test = "ok" 
    current_task.update_state(state='PROGRESS', 
     meta={'test': ok}) 
    return x + y 

在這裏,我的Django的外殼:

>>> from blog.tasks import * 
>>> job = add.delay(2,3) 
>>> job.state 
'PENDING' 
>>> job.result 
>>> 

用我的RabbitMQ界面的圖片: enter image description here

回答

1

您需要啓動一個工人說將處理您添加到隊列中的任務。從你的virtualenv運行:

celery worker -A blog -l info 
+0

謝謝你的快速答案見下文。 – Mammouth

相關問題