Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a non-blocking request with requests when running Flask with Gunicorn and Gevent

Tags:

My Flask application will receive a request, do some processing, and then make a request to a slow external endpoint that takes 5 seconds to respond. It looks like running Gunicorn with Gevent will allow it to handle many of these slow requests at the same time. How can I modify the example below so that the view is non-blocking?

import requests  @app.route('/do', methods = ['POST']) def do():     result = requests.get('slow api')     return result.content 
gunicorn server:app -k gevent -w 4 
like image 971
JLTChiu Avatar asked Sep 28 '16 20:09

JLTChiu


People also ask

How do you use gunicorn with gevent?

By default, Gunicorn uses a synchronous worker class to serve requests, but it can be easily configured to use gevent by simply adding -k gevent to the run command.

Are flasks non-blocking?

If you're deploying your Flask application with gunicorn, it is already non-blocking. If a client is waiting on a response from one of your views, another client can make a request to the same view without a problem. There will be multiple workers to process multiple requests concurrently.

What is gevent in gunicorn?

gevent: Coroutine network library for Python. It is a coroutine -based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libev or libuv event loop; Gunicorn: A Python WSGI HTTP Server for UNIX. Gunicorn is a pre-fork worker model ported from Ruby's Unicorn project.

How many concurrent requests can gunicorn handle?

Yes, with 5 worker processes, each with 8 threads, 40 concurrent requests can be served.


1 Answers

If you're deploying your Flask application with gunicorn, it is already non-blocking. If a client is waiting on a response from one of your views, another client can make a request to the same view without a problem. There will be multiple workers to process multiple requests concurrently. No need to change your code for this to work. This also goes for pretty much every Flask deployment option.

like image 82
sytech Avatar answered Sep 28 '22 01:09

sytech