English 中文(简体)
HTTP call from Celery worker
原标题:

I am running a Flask-Celery server in docker desktop Kubernetes. It uses a Redis Result Backend.

I want to use a Celery Task to make an HTTP call to a program that might take a while. This program returns a result that I want to store somewhere so that the user is able to retrieve it with an HTTP call to Celery.

How do I go about this?
Should I make the Task wait for the HTTP call to finish?
Is this good practice?
How do I make sure the result is stored in the Result Backend?

问题回答

If you control the long-running task you call with an HTTP call, there are several ways to make this work, but whatever way, the celery worker is kind of irrelevant here; it would be the thing to wait until it gets the result to be able to store it under its job result.

back to the long-running task: ideally you make the call, and return immediately. It s not good practice to wait for the long-running task to return; it is better to return a partial result answer, then provide an API endpoint to check for status, and then periodically call for status until the long-running task has finished.

Better yet, you would want to use a 2-way type of connection, like a WebSocket, to establish communication, and request the long-running process to start (with any param/data) and then that long-running process can return the result through that return channel when it is done. Depending on how many connections you expect at any one time, it is more elegant than pinging back for status repeatedly.

No matter what, if the celery job is not the one processing the long-running task, it will just be sitting idle waiting for the results from the service, so is it even useful? You might as well implement the 1st solution in the client needing this result, as it will need to do the same thing asking the celery job if it is done.

Ideally, you move the long-running task into the celery worker itself, and implement client result lookup to celery the way described above.





相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...

热门标签