django jsonrpc ajax,python-在Django中使用Celery设置结果后端(rpc)

我正在尝试让结果后端在我正在处理的项目的本地计算机上工作,但遇到了问题.

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

我正在遵循上述有关配置Celery的教程.以下是来源:

celeryconfig.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'

}

}

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)

task.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 worker时,我确实在config下看到结果:

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

我已经尝试过可以在网上找到的每件事.有没有人可以指出我正确的方向?我非常亲密,对查看此代码非常厌倦.

解决方法:

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'

)

标签:celery,python,django

来源: https://codeday.me/bug/20191026/1938450.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django是一个用于快速开发Web应用程序的Python Web框架。而python-docx-template是一个Python库,它可以使用Word文档作为模板,然后根据传入的数据批量生成Word文档。在Django,我们可以利用python-docx-template库来实现批量生成Word文档的功能。 首先,我们需要在Django项目安装python-docx-template库。可以使用pip命令来安装该库: ```bash pip install python-docx-template ``` 接下来,我们可以在Django项目创建一个视图函数,用于接收数据并根据模板生成Word文档。在视图函数,我们可以使用python-docx-template库提供的方法将数据填充到Word模板,生成最终的Word文档。 例如,假设我们有一个Word文档模板`template.docx`,里面包含了一些需要填充数据的位置,我们可以在Django这样写视图函数: ```python from docxtpl import DocxTemplate from django.http import HttpResponse def generate_word_document(request): # 从请求获取数据 data = request.GET.get('data', '') # 读取Word模板 doc = DocxTemplate("template.docx") # 根据数据填充模板 context = {'data': data} doc.render(context) # 写入生成的Word文档 doc.save("generated_document.docx") # 返回生成的Word文档给用户 with open("generated_document.docx", 'rb') as f: response = HttpResponse(f.read(), content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document') response['Content-Disposition'] = 'attachment; filename=generated_document.docx' return response ``` 通过上述视图函数,我们可以在Django项目实现批量生成Word文档的功能,用户可以通过传入数据来生成他们所需的Word文档。这样我们就可以方便地利用PythonDjango来批量生成Word文档,提高生产效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值