vinllen
vinllen

Reputation: 1459

Gevent blocked by flask even use monkey patch

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),
        ]) 

Upvotes: 3

Views: 3362

Answers (1)

davidism
davidism

Reputation: 127190

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.

Upvotes: 4

Related Questions