在江西用芹菜建立结果后端(rpc)

2024-04-26 20:44:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图得到一个结果后端工作在我的本地计算机上的一个项目,我正在工作,但我遇到了一个问题。

目前我正在尝试创建队列系统,以便我的实验室创建案例。这是为了防止使用重复的序列号。我已经在用芹菜印刷,所以我想我会创建一个新的芹菜队列,并用它来处理这个案件。前端还需要获得案例创建的结果,以显示创建的案例号。

http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#rabbitmq

我正在按照上面的教程来配置我的芹菜。以下是来源:

芹菜配置.py:

from kombu import Queue
CELERY_DEFAULT_QUEUE = 'celery'
CELERY_DEFAULT_EXCHANGE = 'celery'
CELERY_DEFAULT_EXCHANGE_TYPE = 'direct'
CELERY_RESULT_BACKEND = 'rpc://'
CELERY_RESULT_PERSISTENT = False

CELERY_QUEUES = (
    Queue('celery',    routing_key="celery"),
    Queue('case_creation',       routing_key='create.#')
)

CELERY_ROUTES = {
    'case.tasks.create_case': {
        'queue': 'case_creation',
        'routing_key': 'create.1'
    },
    'print.tasks.connect_and_serve': {
        'queue': 'celery',
        'routing_key': 'celery'
    }
}

芹菜.py:

import os

from celery import Celery

from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings.local')

app = Celery('proj', broker='amqp://guest@localhost//')

app.config_from_object('proj.celeryconfig')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

任务.py:

import celery
from django.db import IntegrityError

from case.case_create import CaseCreate


@celery.task(bind=True)
def create_case(self, data, user, ip):
    try:
        acc = CaseCreate(data, user, ip)
        return acc.begin()
    except IntegrityError as e:
        self.retry(exc=e, countdown=2)

以下是我的观点,称之为上述任务:

@require_authentication()
@requires_api_signature()
@csrf_exempt
@require_http_methods(['POST'])
def api_create_case(request):
    result = create_case.delay(json.loads(request.body.decode('utf-8')), request.user, get_ip_address(request))
    print(str(result))  # Prints the Task ID
    print(str(result.get(timeout=1)))  # Throws error
    return HttpResponse(json.dumps({'result': str(result)}), status=200)

我使用以下命令启动芹菜队列:

celery -A proj worker -Q case_creation -n case_worker -c 1

当我运行芹菜工人时,我确实在配置下看到了结果:

 -------------- celery@case_worker v3.1.16 (Cipater)
---- **** -----
--- * ***  * -- Windows-8-6.2.9200
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         proj:0x32a2990
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     rpc://
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ----
--- ***** ----- [queues]
 -------------- .> case_creation    exchange=celery(direct) key=create.#

当我运行程序并提交新案例时,这是我收到的错误消息:

No result backend configured.  Please see the documentation for more information.

我已经尝试了我在网上能找到的每一件事。有没有人能指引我正确的方向?我是如此的接近,如此的厌倦了看这个代码。


Tags: keyfromimportapprequestcreateresultrouting
1条回答
网友
1楼 · 发布于 2024-04-26 20:44:51

如果你想保留你的结果,试试这个Keeping Results

app = Celery('proj', backend='amqp', broker='amqp://guest@localhost//')

编辑

Make sure the client is configured with the right backend.

If for some reason the client is configured to use a different backend than the worker, you will not be able to receive the result, so make sure the backend is correct by inspecting it:

尝试查看输出:

>>> result = task.delay(…)
>>> print(result.backend)

其他解决方案将取代

app = Celery('proj',
         backend='amqp',
         broker='amqp://',
         include=['proj.tasks'])

尝试:

app = Celery('proj',
             broker='amqp://',
             include=['proj.tasks'])
app.conf.update(
    CELERY_RESULT_BACKEND='amqp'
)

相关问题 更多 >