Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running flask + gevent + requests not serving 'concurrently'

I kick off my flask app like this:

#!flask/bin/python
from app import app_instance
from gevent.pywsgi import WSGIServer

#returns and instance of the application - using function to wrap configuration
app = app_instance()
http_server = WSGIServer(('',5000), app)
http_server.serve_forever()

And then when I try to execute this code, the requests call blocks until the original request times out. I'm basically invoking a webservice in the same flask app. What am I misunderstanding about gevent? Wouldn't the thread yield when an i/o event occurred?

@webapp.route("/register", methods=['GET', 'POST'])
def register():
    form = RegistrationForm(request.form, csrf_enabled=False)
    data = None
    if request.method == 'POST' and form.validate():
        data= {'email': form.email, 'auth_token': form.password,
                'name' : form.name, 'auth_provider' : 'APP'}
        r = requests.post('http://localhost:5000', params=data)
        print('status' + str(r.status_code))
        print(r.json())
    return render_template('register.html', form=form)
like image 950
fansonly Avatar asked Jan 27 '13 20:01

fansonly


People also ask

Can Flask handle concurrent requests?

Improve performance in both Blocking and Non-Blocking web servers. Multitasking is the ability to execute multiple tasks or processes (almost) at the same time. Modern web servers like Flask, Django, and Tornado are all able to handle multiple requests simultaneously.

Can Wsgi handle multiple requests?

When multiple requests arrive a WSGI server uses a thread pool to create a new thread which will handle the request and return the response. The scope of this thread would be for the single request. Context switching between threads is handled by the OS.

How does Flask handle multiple concurrent requests?

Flask applications are deployed on a web server, either the built-in server for development and testing or a suitable server (gunicorn, nginx etc.) for production. By default, the server can handle multiple client requests without the programmer having to do anything special in the application.

How many requests can Flask handle at a time?

For reference, the Flask benchmarks on techempower give 25,000 requests per second.


1 Answers

I believe the issue is likely that you forgot to monkey patch. This makes it so that all of the normally blocking calls become non-blocking calls that utilize greenlets. To do this just put this code before you call anything else.

from gevent import monkey; monkey.patch_all()

Go to http://www.gevent.org/intro.html#monkey-patching for more on this.

like image 181
ravenac95 Avatar answered Oct 27 '22 13:10

ravenac95