【发布时间】:2016-11-16 00:59:59
【问题描述】:
在烧瓶应用程序中,我需要在执行return render_template(page) 后执行其他任务的checkJob 功能(检查作业状态和电子邮件给用户)。用户将看到确认页面,但仍有后台作业正在运行以检查作业状态。
我尝试将 celery https://blog.miguelgrinberg.com/post/using-celery-with-flask 用于后台作业,但它不起作用。 return render_template(page) 之后的任何内容都不会被执行。
代码片段如下:
@app.route("/myprocess", methods=['POST'])
def myprocess():
//.... do work
#r = checkJob()
return render_template('confirm.html')
r = checkJob()
@celery.task()
def checkJob():
bb=1
while bb == 1:
print "checkJob"
time.sleep(10)
【问题讨论】:
-
你不能在
return之后运行任何东西,因为return结束(每个)函数,它会立即离开函数。您必须在return之前使用checkJob.delay() -
谢谢@furas。我尝试将 checkJob.delay() 放在 return 语句之前,但问题是没有显示 confirm.html 并等待 checkJob 完成。有没有办法可以立即显示 confirm.html 然后执行 checkJob 任务?非常感谢!!
-
如我所见,您还没有尝试使用 Celery。
-
谢谢@Sergey。你怎么知道我没有使用 Celery?你能告诉我我犯了什么错误吗?我按照blog.miguelgrinberg.com/post/using-celery-with-flask 中的示例进行操作
-
如果您阅读了那篇文章,为什么不使用
.delay()或.apply_async()调用 celery 任务?
标签: python flask celery celery-task