【问题标题】:Running flask + gevent + requests not serving 'concurrently'运行烧瓶 + gevent + 请求未“同时”服务
【发布时间】:2013-01-27 20:23:08
【问题描述】:

我像这样启动我的烧瓶应用程序:

#!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()

然后,当我尝试执行此代码时,请求调用会阻塞,直到原始请求超时。我基本上是在同一个烧瓶应用程序中调用网络服务。我对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)

【问题讨论】:

    标签: python io flask gevent


    【解决方案1】:

    我相信这个问题很可能是你忘记了猴子补丁。这使得所有正常阻塞的调用都变成了利用greenlets的非阻塞调用。要做到这一点,只需将此代码放在 您调用其他任何内容之前。

    from gevent import monkey; monkey.patch_all()
    

    请转至http://www.gevent.org/intro.html#monkey-patching 了解更多信息。

    【讨论】:

    • 顺便说一句.. 我是 python 和猴子补丁的新手。为什么服务器本身不能修补所有内容?或者这只是样式问题,因为猴子修补应该显式而不是隐式发生?
    • @user1924221,gevent 不知道你需要什么猴子补丁。有时您可能不希望对所有内容进行修补。 Gevent 和 flask 也是独立的,所以你必须手动完成这些事情。当然,您可以编写自己的库来执行此操作,这样您就不必再手动执行此操作了。
    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2013-08-19
    • 2014-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多