django芹菜SQS"没有配置结果后端。"

时间:2018-05-31 10:24:23

标签: django amazon-web-services celery python-3.6 amazon-sqs

不使用Celery: No Result Backend Configured?,因为使用了SQS。

继续收到以下错误:

  

未配置任何结果后端。有关更多信息,请参阅文档   信息。

我的制作设置如下:

CELERY_BROKER_URL = 'sqs://%s:%s@' % (
    urllib.parse.quote(env.str('TASK_QUEUE_USER_ID'), safe=''),
    urllib.parse.quote(env.str('TASK_QUEUE_USER_SECRET'), safe=''))

BROKER_URL = CELERY_BROKER_URL
CELERY_ENABLE_REMOTE_CONTROL = False
CELERY_RESULT_BACKEND = None # Disabling the results backend
RESULT_BACKEND = None # Disabling the results backend

CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_DEFAULT_QUEUE = 'async_tasks'
SQS_QUEUE_NAME = 'async_tasks'
CELERY_ENABLE_REMOTE_CONTROL = False 
CELERY_SEND_EVENTS = False


CELERY_BROKER_TRANSPORT_OPTIONS = {
    'region': 'eu-west-2',
    'polling_interval': 3,
    'visibility_timeout': 3600,
}
CELERY_SEND_TASK_ERROR_EMAILS = True

#
# https://stackoverflow.com/questions/8048556/celery-with-amazon-sqs#8567665
#
CELERY_BROKER_TRANSPORT = 'sqs'
BROKER_TRANSPORT = 'sqs'

从命令行运行芹菜: DJANGO_ENV=production celery -A async_tasks worker -l info 连接到SQS并进行轮询,但是当我尝试从命令行DJANGO_ENV=production python manage.py check_async进行演示调用时:

from django.core.management.base import BaseCommand, CommandError

import async_tasks.tasks as tasks


class Command(BaseCommand):
    help = 'Check if infrastructure for async tasks has been setup correctly.'

    def handle(self, *args, **options):
        try:
            print('Sending async request.')
            t = tasks.add.apply_async((2, 4))
            out = t.get(timeout=1)
            print(out)
            print(t.status)
        except Exception as e:
            print(e)
            raise CommandError('Error occured')

我收到上面的错误。尝试使用redis开发机器,一切运行良好。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您需要将Celery Result后端配置为能够存储和收集任务结果。可以将Celery与不带结果后端的SQS代理一起使用,可以“解雇”模式,但是如果您希望能够通过get()之类的方法访问任务的结果,那还不够。

答案 1 :(得分:1)

也许这会帮助某人。上面的答案是正确的,但是如果您仍然想使用Django,SQS和Celery,并且仍然想要查看结果,则可以通过使用django-celery-results库将Django的ORM或Cache Framework用作后端。

Django-celery-results

Celery Documentation - ORM Cache Framework