Running flask + gevent + requests not serving 'concurrently'

fansonly picture fansonly · Jan 27, 2013 · Viewed 11.1k times · Source

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)

Answer

ravenac95 picture ravenac95 · Jan 27, 2013

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.