【发布时间】:2017-12-08 22:11:15
【问题描述】:
问题:我正在使用带有 celery 的 rabbitmq 服务器
- 我需要在后台运行多个任务。我能做到的
- 当每个任务完成后,我需要渲染 html 页面,运行 html 页面我需要请求对象作为参数,celery 任务无法访问
- 当 celery 任务完成时,我已经脱离了具有请求对象的函数
从 未来 导入 unicode_literals、absolute_import 从 django.shortcuts 导入渲染 从 celDemo.celery 导入应用程序 进口时间 从 django.http.response 导入 HttpResponseRedirect # 在此处创建您的视图。
dictionary = {}
msg = "Hello!"
def testing(request):
print 'In 1st page:'
return render(request,'index.html')
def testcelery(request):
print 'In testcelery'
if request.method == 'POST':
print 'On click event'
value = str(request.POST['variable'])
print request
dictionary[value] = request
print dictionary[value]
try:
make_wait.delay(value)
return render(request,'index.html')
except:
return render(request,'nothing.html')
@app.task
def make_wait(value):
print dictionary
print msg
print 'Started sleeping :)'
actual2()
print 'Slept :D'
#request = dictionary[value]
print value
print 'before initiaing the request'
return render('nothing.html')
print 'after initiating the request'
#return value
def actual2():
result = actual()
return result
def actual():
time.sleep(25)
return 'slept :D'
【问题讨论】:
标签: django python-2.7 django-views rabbitmq django-celery