我像这样启动我的烧瓶应用程序:
#!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()
然后,当我尝试执行此代码时,请求调用将阻塞,直到原始请求超时为止。我基本上是在同一烧瓶应用程序中调用Web服务。我对gevent有什么误解?发生I / O事件时线程不会屈服吗?
@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)
我相信这个问题很可能你忘记了monkey patch。这样一来,所有正常阻塞的呼叫都将变为利用greenlet的非阻塞呼叫。为此,只需在调用其他代码之前放置此代码即可。
from gevent import monkey; monkey.patch_all()