Safe use of Thread when processing a request? #1997
Replies: 2 comments
-
|
If you are using the With the |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the really quick reply. Celery looks /waaay/ too heavy for what I need. I found some useful discussion here: and that suggested using https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task to create a fire-and-forget IO-bound task, which is exactly what I have here. Anyhow many people have asked similar questions, I will post a few links: https://stackoverflow.com/questions/48994440/execute-a-function-after-flask-returns-response I was just concerned about messing up the Connexion v3, ASGI and uvicorn infrastructure. It doesn't sound like creating a new thread poses any problems for that infrastructure. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Please advise on proper/safe use of threads in Connexion V3.
Here's the context/requirement. My connexion-based Flask-using REST server accepts a request to interact with a remote system that is slow. My REST server must ack the request immediately, wait for the remote system to finish, then eventually POST back results to a URL supplied in the original request. I don't think this is especially novel or unusual.
I implemented it like this: the function that processes the inbound request starts a Python thread then returns the ack, which I believe finishes the request-response lifecycle. The thread wraps a function that POSTs the data from the request to a remote system, waits for the remote system to answer, then POSTs data back to the caller-supplied URL; then the thread exits.
In the migration guide I saw a warning about use of threads in the context of the new ASGI server, so I figured it might be smart to ask and avoid surprises.
Please comment, thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions