代码之家  ›  专栏  ›  技术社区  ›  William Ross

与Gunicorn一起使用的gevent pywsgi服务器?

  •  1
  • William Ross  · 技术社区  · 6 年前

    我有一个flask应用程序设置,可以在我的代码中使用gevent wsgiserver。我还在命令行上运行gunicorn来启动服务器。

    在使用gunicorn运行时,是否应该在代码中使用wsgi服务器?目前的情况如下:

    from flask import Flask
    from gevent.pywsgi import WSGIServer
    
    application = Flask(__name__)
    
    @application.route("/")
    def hello():
        return "hello"
    
    if __name__ == '__main__':
        port = int(os.environ.get('PORT', 5000))
        WSGIServer(('', port), application).serve_forever()
    

    在命令行上,我正在运行Gunicorn,就像:

    gunicorn -w 4 myapp:application
    

    我的代码中是否需要wsgiserver,或者只是在默认的flask服务器上以application.run()的形式运行它?

    1 回复  |  直到 6 年前
        1
  •  1
  •   William Ross    6 年前

    根据 Standalone WSGI Containers ,gunicorn和gevent.pywsgi都是wsgi容器,gunicorn只重新定义名为 应用 .
    所以下面的代码 if __name__ == '__main__': 不再有用。
    如果要使用gevent,可以执行以下操作:

    gunicorn -k gevent -w 4 myapp:application