theMoonlitKnight
theMoonlitKnight

Reputation: 451

Flask: asynchronous response to client

I'm using Flask to develop a web server in a python app. I'm achieving this scenario: the client (it won't be a browser) sends a request, the server does some long task in background and on completion sends the response back to the client asynchronously. Is it possible to do that?

Upvotes: 6

Views: 3755

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

What you ask cannot be done with the HTTP protocol. Each request receives a response synchronously. The closest thing to achieve what you want would be this:

The client sends the request and the server responds with a job id immediately, while it also starts a background task for this long calculation.

The client can then poll the server for status by sending the job id in a new request. The response is again immediate and contains a job status, such as "in progress", "completed", "failed", etc. The server can also return a progress percentage, which the client can use to render a progress bar.

You could also implement web sockets, but that will require socket enabled server and client.

Upvotes: 7

Related Questions