简体   繁体   English

Flask 引发在 Gunicorn 等 WSGI 服务器上运行的“地址已在使用”

[英]Flask raises `Address already in use` running with a WSGI server such as Gunicorn

I'm trying to run my app with Gunicorn.我正在尝试使用 Gunicorn 运行我的应用程序。 However, Flask raises OSError: [Errno 98] Address already in use while Gunicorn is starting, then Gunicorn shuts down.但是,Flask 在 Gunicorn 启动时引发OSError: [Errno 98] Address already in use ,然后 Gunicorn 关闭。 How do I serve the app with Gunicorn?如何使用 Gunicorn 为应用程序提供服务?

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello, World!'

app.run(debug=True)
gunicorn app:app
[2017-02-19 21:09:50 -0800] [21965] [INFO] Starting gunicorn 19.6.0
[2017-02-19 21:09:50 -0800] [21965] [INFO] Listening at: http://127.0.0.1:8000 (21965)
[2017-02-19 21:09:50 -0800] [21965] [INFO] Using worker: sync
[2017-02-19 21:09:50 -0800] [21968] [INFO] Booting worker with pid: 21968
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
[2017-02-19 21:09:50 -0800] [21969] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
  File "/home/david/Projects/py36/app.py", line 4, in <module>
    app.run(debug=True)
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/home/david/.virtualenvs/py36/lib/python3.6/site-packages/werkzeug/serving.py", line 691, in run_simple
    s.bind((hostname, port))
OSError: [Errno 98] Address already in use

[2017-02-19 21:09:50 -0800] [21968] [INFO] Worker exiting (pid: 21968)
[2017-02-19 21:09:50 -0800] [21965] [INFO] Shutting down: Master
[2017-02-19 21:09:50 -0800] [21965] [INFO] Reason: Worker failed to boot.

I tried gunicorn Connection in use for python flask and error: [Errno 98] Address already in use but couldn't get it to work.我尝试了用于 python 烧瓶的 gunicorn Connection出现错误:[Errno 98] 地址已在使用中,但无法正常工作。

You're using Gunicorn (or any production WSGI server), so you don't want to use the Flask dev server.您正在使用 Gunicorn(或任何生产 WSGI 服务器),因此您不想使用 Flask 开发服务器。 But you're calling app.run unconditionally.但是你无条件地调用app.run Gunicorn starts, binds the address, then imports your app, which calls app.run and tries to start its own server. Gunicorn 启动,绑定地址,然后导入您的应用程序,该应用程序调用app.run并尝试启动自己的服务器。 But the address is already in use by Gunicorn.但该地址已被 Gunicorn 使用。

Move app.run into a guard block:app.run移动到保护块中:

if __name__ == '__main__':
    app.run()

Or preferably remove it completely, since you should be using the flask command to run the dev server, as described in the docs .或者最好完全删除它,因为您应该使用flask命令来运行开发服务器,如docs中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask make_server 总是引发“OSError:[Errno 98] 地址已在使用中” - Flask make_server always raises "OSError: [Errno 98] Address already in use" 烧瓶服务器启动问题 - 地址已在使用中 - Flask server starting issue - Address already in use 带有 wsgi 的 gunicorn 问题的烧瓶 - Flask with gunicorn problems with wsgi 使用gunicorn作为wsgi服务器将请求记录到Flask服务器上……到AWS CloudWatch - Log requests to a flask server using gunicorn as wsgi server … to AWS cloudwatch 在运行 WSGI 服务器之前在 gunicorn 中运行设置脚本 - Running setup script in gunicorn prior to running WSGI server 在另一个进程中运行烧瓶时地址已被使用 - Address already in use when running flask in another process 如何在烧瓶上使用带有 swagger_server 的 gunicorn - how to use gunicorn with swagger_server on flask 使用Gunicorn运行Flask引发TypeError:index()获取0个位置参数,但给出了2个 - Running Flask with Gunicorn raises TypeError: index() takes 0 positional arguments but 2 were given 当我在gunicorn上运行Flask应用程序时设置app.wsgi_app = ProxyFix(app.wsgi_app)会发生什么? - What is going on when I set app.wsgi_app = ProxyFix(app.wsgi_app) when running a Flask app on gunicorn? Gunicorn Python 3.7 Flask 1.1 出现 WSGI 错误 - WSGI Error with Gunicorn Python 3.7 Flask 1.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM