[英]How to get data from django celery tasks
# In tasks.py file
from __future__ import absolute_import
from celery import shared_task
@shared_task
def add(x, y):
return x + y
# In views.py
def home(request):
context = {
'add': tasks.add.delay(5,2)
}
return render(request, "home.html", context)
# In home.html
<h1>{{ add }}</h1>
Instead of 7 appearing on home.html I get giberish that looks like this:313e-44fb-818a-f2b2d824a46b. 而不是7出现在home.html上,我看起来像这样的giberish:313e-44fb-818a-f2b2d824a46b。 What can I do to get the data from my tasks.py file?
如何从tasks.py文件中获取数据?
Simply change: 只需更改:
def home(request):
context = {
'add': tasks.add.delay(5,2)
}
return render(request, "home.html", context)
to: 至:
def home(request):
context = {
'add': tasks.add.delay(5,2).get()
}
return render(request, "home.html", context)
This will be processed by Celery too. 这也将由Celery处理。
Ading .delay()
You setting task as asynchronous and You receiving task_id
. Ading
.delay()
您将任务设置为异步并且您正在接收task_id
。
More info in doc: Calling Tasks doc中的更多信息: 调用任务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.