【问题标题】:How will I know if celery background process is successful inside django code. If it is successful I want render a html page我如何知道 celery 后台进程在 django 代码中是否成功。如果成功我想渲染一个html页面
【发布时间】:2017-12-10 01:13:19
【问题描述】:

我做不到,因为我在 django 代码中没有当前正在运行的后台进程的状态。

请求: 我需要知道 celery 后台任务的状态并从那里渲染 html 页面

@task_success.connect
def task_sent_handler3(sender=None,result=None,**kwargs):
    # information about task are located in headers for task messages
    # using the task protocol version 2.
    #info = headers if 'task' in headers else body
    tester()
    #print('after_task_publish for task id {info[id]}'.format(info="hhhi",))

我尝试了上面的 celery 注释,但我没有返回任何东西。 如果它也返回,我将能够访问局部变量并返回渲染函数

render(request,'page.html') 来自该成功装饰函数

我该如何解决这个问题?

【问题讨论】:

  • 在哪里渲染?背景就是这样,在后台。应该为哪个潜在客户呈现 html 页面?

标签: django python-2.7 django-views celery django-celery


【解决方案1】:

当您为每个任务运行任务时,都会有唯一的 ID。您可以从该 ID 跟踪任务的状态。在前端,您可以检查为该 ID 发送 ajax 调用的状态。当它完成后,您可以显示您想要显示的任何内容。

http://thoslin.github.io/async-download-with-celery/

【讨论】:

  • 感谢您的链接.. 我是 Ajax 新手,我的后台任务需要 1 小时才能完成,我可以让 Ajax 调用等到 Task.Ready() 返回 true。当它返回true时,我想调用一个html页面。您能否分享一下满足我需求的 Ajax 调用...谢谢
猜你喜欢
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2014-07-07
  • 1970-01-01
相关资源
最近更新 更多