2017-02-19 46 views
3

我試圖用Gunicorn運行我的應用程序。然而,當Gunicorn開始時,Flask提高OSError: [Errno 98] Address already in use,然後Gunicorn關閉。如何與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. 

我試圖gunicorn Connection in use for python flaskerror: [Errno 98] Address already in use但不能讓它開始工作。

回答

3

您正在使用Gunicorn,因此您不想使用Flask dev服務器。但你無條件地致電app.run。 Gunicorn啓動,綁定地址,然後導入您的應用程序,該應用程序調用app.run並嘗試啓動它自己的服務器。但該地址已被Gunicorn使用。

移動app.run成保護塊:

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

或者優選地完全去除它,因爲你應該使用flask命令運行開發服務器如在docs說明。