2

I am running a compute-heavy Flask app behind gunicorn. Requests are handled by Nginx that passes them onto gunicorn and the Flask app:

gunicorn -access-logfile - wsgi --bind 127.0.0.1:5002 --workers 2 --threads 8

Each request takes about 1 second to process (CPU-heavy). If two requests arrive in close succession, I would like the second request to "wait", while the first is being processed. But instead it seems that both requests get slower as the CPU attempts to process both in parallel.

Is there a way to force the app to process incoming requests sequentially?

Thank you!

David
  • 1,898
  • 2
  • 14
  • 32

1 Answers1

2

You can use python-filelock to achieve your goal, try to acquire the same lock on all processes/threads before running your CPU-heavy code.

But I am not sure why do you use --workers 2 --threads 8. This answer to the question "How many concurrent requests does a single Flask process receive?" , might also provide an answer to your usecase

ofirule
  • 4,233
  • 2
  • 26
  • 40