停止线程,直到芹菜任务完成

时间:2022-08-23 18:08:56

I have a django webserver, and a form in which the user enters information. Everytime the form information changes I update the model in my database, and at a certain point when something validates I will create a long running task in celery to get my results even before the user clicked next.

我有一个django网络服务器,以及一个用户输入信息的表单。每次表单信息更改时,我都会在我的数据库中更新模型,并且在某些时候验证某些内容时,我会在celery中创建一个长时间运行的任务,以便在用户单击下一步之前获得我的结果。

I am using Django Celery with RabbitMQ as broker, and my question is what is the most appropriate way of IN CASE the task is still not finished to just lock the response thread in django until the task is state.SUCCESSFUL I tried using the AsyncResult.get method for that, but it just locks the thread for a real long time and then gives me the result. IE It's not instant, does anyone have an idea how to solve this?

我使用Django Celery和RabbitMQ作为代理,我的问题是什么是最合适的IN CASE方式,任务仍然没有完成只是锁定django中的响应线程,直到任务是state.SUCCESSFUL我尝试使用AsyncResult。获取方法,但它只是锁定线程很长一段时间,然后给我结果。 IE它不是即时的,有没有人知道如何解决这个问题?

2 个解决方案

#1


2  

You can just wait until the result is ready().

你可以等到结果准备就绪()。

from time import sleep
result = some_task.apply_async(args=myargs)
while not result.ready():
    sleep(0.5)
result_output = result.get()

It appears there is also a wait(), so you could just use that. The following should is basically doing the same thing as the code above.

它似乎还有一个wait(),所以你可以使用它。以下应基本上与上面的代码做同样的事情。

result = some_task.apply_async(args=myargs)
result_output = result.wait(timeout=None, interval=0.5)

#2


1  

one way to accomplish that would be to have the results waiting in redis, and get them using a blocking pop operation using some unique value like session id, note its timeout capability.

实现这一目标的一种方法是让结果在redis中等待,并使用阻塞弹出操作使用某些唯一值(如会话ID)来获取它们,请注意其超时功能。

#1


2  

You can just wait until the result is ready().

你可以等到结果准备就绪()。

from time import sleep
result = some_task.apply_async(args=myargs)
while not result.ready():
    sleep(0.5)
result_output = result.get()

It appears there is also a wait(), so you could just use that. The following should is basically doing the same thing as the code above.

它似乎还有一个wait(),所以你可以使用它。以下应基本上与上面的代码做同样的事情。

result = some_task.apply_async(args=myargs)
result_output = result.wait(timeout=None, interval=0.5)

#2


1  

one way to accomplish that would be to have the results waiting in redis, and get them using a blocking pop operation using some unique value like session id, note its timeout capability.

实现这一目标的一种方法是让结果在redis中等待,并使用阻塞弹出操作使用某些唯一值(如会话ID)来获取它们,请注意其超时功能。