fan*_*nly 10 python io gevent flask
我这样开瓶我的烧瓶应用程序:
#!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()
Run Code Online (Sandbox Code Playgroud)
然后,当我尝试执行此代码时,请求会调用阻塞,直到原始请求超时.我基本上是在同一个烧瓶应用程序中调用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)
Run Code Online (Sandbox Code Playgroud)
rav*_*c95 18
我相信这个问题可能是你忘记了补丁.这使得所有正常阻塞调用成为使用greenlet的非阻塞调用.要做到这一点,只需在调用其他任何内容之前输入此代码.
from gevent import monkey; monkey.patch_all()
Run Code Online (Sandbox Code Playgroud)
有关此内容的更多信息,请访问http://www.gevent.org/intro.html#monkey-patching.