我这样启动我的 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()
然后,当我尝试执行此代码时,请求调用会阻塞,直到原始请求超时。我基本上是在同一个 flask 应用程序中调用网络服务。我对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)
最佳答案
我认为问题很可能是您忘记了猴子补丁。这使得所有正常阻塞的调用都变成了使用 greenlet 的非阻塞调用。要做到这一点,只需将这段代码放在 你调用其他任何东西之前。
from gevent import monkey; monkey.patch_all()
关于python - 运行 flask + gevent + 请求不服务 'concurrently',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551823/