Patruni Srikanth
Patruni Srikanth

Reputation: 769

Handling time consuming requests in Flask-UWSGI app

Am running an app with Flask , UWSGI and Nginx. My UWSGI is set to spawn out 4 parallel processes to handle multiple requests at the same time. Now I have one request that takes lot of time and that changes important data concerning the application. So, when one UWSGI process is processing that request and say all others are also busy, the fifth request would have to wait. The problem here is I cannot change this request to run in an offline mode as it changes important data and the user cannot simply remain unknown about it. What is the best way to handle this situation ?

Upvotes: 1

Views: 1167

Answers (1)

Igor
Igor

Reputation: 2909

As an option you can do the following:

  1. Separate the heavy logic from the function which is being called upon @route and move it into a separate place (a file, another function, etc)
  2. Introduce Celery to run that pieces of heavy logic (it will be processed in a separate thread from the @route-decorated functions). A quick way of doing this is using Redis as a message broker.
  3. Schedule the time-consuming functions from your @route-decorated functions in Celery (it is possible to pass parameters as well)

This way the HTTP requests won't be blocked for the complete function execution time.

Upvotes: 1

Related Questions