运行flask + gevent +请求不能同时服务 [英] Running flask + gevent + requests not serving 'concurrently'
问题描述
#!flask / bin / python
from app import app_instance $ b $ from gevent.pywsgi import WSGIServer
应用程序的返回和实例 - 使用函数来包装配置
app = app_instance()
http_server = WSGIServer(( ',5000),app)
http_server.serve_forever()
然后当我尝试执行此代码,请求调用块,直到原始请求超时。我基本上是在同一个烧瓶应用程序调用Web服务。我对gevent有什么误解?当发生I / O事件时线程是不会产生的?
('GET','POST'])def register():
form = RegistrationForm(request.form,csrf_enabled = False)
data = None
request.method =='POST'和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)
我相信这个问题很可能是你忘了猴子补丁。这使得所有通常阻塞的调用都成为利用greenlet的非阻塞调用。要做到这一点,只需要在之前的这个代码调用其他任何东西即可。
from gevent import monkey; monkey.patch_all()
转至http://www.gevent.org/intro.html#monkey-patching 了解更多。
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)
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.
这篇关于运行flask + gevent +请求不能同时服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!