13

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)
Paco
  • 4,520
  • 3
  • 29
  • 53
fansonly
  • 1,150
  • 4
  • 14
  • 29

1 Answers1

23

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.

ravenac95
  • 3,557
  • 1
  • 20
  • 21
  • As an aside.. I'm new to python and monkey patching. Why can't the server itself monkey patch everything? or is that just a matter of style as monkey patching should happen explicitly rather than implictly? – fansonly Jan 27 '13 at 21:53
  • @user1924221, gevent isn't aware of what you need to monkey patch. Sometimes you may not wish to monkey patch everything. Gevent and flask are independent as well so you have to do these things manually. Of course, you could write your own lib to do it so you never have to do it manually again. – ravenac95 Jan 27 '13 at 21:55