Gevent blocked by flask even use monkey patch

╄→гoц情女王★ 提交于 2019-12-10 18:24:12

问题


I'm using the flask+gevent to build my server, but the gevent named 'getall' was blocked by flask, so the 'getall' function cannot print message in this code. The monkey patch is in use.

import time
import WSGICopyBody
from flask import Flask
import gevent

def handle_node_request() :
    while True :
        print 'in handle_node_request'
        gevent.sleep(1)

def getall() :
    print 'in getall'

def create_app() :
    app = Flask(__name__)

    app.wsgi_app = WSGICopyBody.WSGICopyBody(app.wsgi_app)
    app.add_url_rule('/node',
                     'handle_node_request',
                     handle_node_request,
                     methods=['GET', 'PUT', 'POST', 'DELETE'])
    return app

if __name__ == "__main__":
    app = create_app()
    from gevent import monkey
    monkey.patch_all()
    gevent.joinall([
            gevent.spawn(app.run(host='0.0.0.0', port=8899, debug=True)),
            gevent.spawn(getall),
        ]) 

回答1:


You need to pass the function and arguments to spawn which will call the function with those arguments in the separate eventlet, but right now you are actually calling run, which never ends until you kill it.

gevent.spawn(app.run, host='0.0.0.0', port=8899, debug=True)

On a side note, this does not seem like the right way to run Flask with Gevent. The Flask docs describe using WSGIServer. Additionally, you should use a real app server in production (that is, when you're not running on 'localhost'). Gunicorn and uWSGI are both capable of using Gevent to handle requests.



来源:https://stackoverflow.com/questions/32458568/gevent-blocked-by-flask-even-use-monkey-patch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!