I deployed my Flask application according to the guidance of How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 14.04. It can be deployed successfully using the simple examples in the tutorial, but it does not work when replacing the application's entry file with my own. , and the strange thing is that
is used directly in the virtualenv environmentpython wsgi.py
is possible, but use
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi
does not work, the error is as follows:
dev@ubuntu:~/tests$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi
*** Starting uWSGI 2.0.12 (64bit) on [Sat Apr 9 16:11:05 2016] ***
compiled with version: 4.8.2 on 08 April 2016 16:57:14
os: Linux-3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014
nodename: ubuntu
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /home/xiaoyi/tests
detected binary path: /home/xiaoyi/tests/env/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 7733
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3
Python version: 2.7.6 (default, Jun 22 2015, 18:01:27) [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xb98500
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
File "./wsgi.py", line 17, in <module>
app.run(host=os.getenv('IP', '0.0.0.0'), port = int(os.getenv('PORT',5000)))
File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 694, in run_simple
inner()
File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 656, in inner
fd=fd)
File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 550, in make_server
passthrough_errors, ssl_context, fd=fd)
File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 464, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()
File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in use
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 13702, cores: 1)
^A--- no python application found, check your startup logs for errors ---
[pid: 13702|app: -1|req: -1/1] 14.28.139.49 () {34 vars in 644 bytes} [Sat Apr 9 16:12:50 2016] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)
Although the above error message indicates that the Address has been used, multiple addresses and ports that occupy the address and port have not been opened. I don’t understand this at first, please give me some guidance!
某草草2017-05-16 17:19:52
After careful investigation, I finally solved the problem. First, I’ll post the code of my entry file:
#!/usr/bin/env python
# -*- coding=utf-8 -*-
from application import create_app
__author__ = 'Riky'
app = create_app('idc')
app.run()
When executed directly using uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi
, the error reported is the same as the error message posted in the question, and the address is occupied. But the corresponding application that is occupied cannot be found in the process or port. Why is a simple example OK? I compared it carefully:
from flask import Flask
application = Flask(__name__)
@application.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == "__main__":
application.run(host='0.0.0.0')
Obviously because I am used to developing in IDE, I ignored the most basic part of the entry file:
#入口在没有以下代码的前提下,使用python run.py 是可以执行的
if __name__ == "__main__":
app.run(host='0.0.0.0')
The error becomes:
*** Operational MODE: single process ***
unable to load app 0 (mountpoint='') (callable not found or import error)
The occurrence of this error is a rather speechless problem, uwsgi can only be recognized in the entry fileapplication
,而无法识别我定义的run
。之所以会出现端口占用的情况,是因为app.run()
实际上也执行了,但并不是uwsgi要加载的应用application
.
Finally just change it to:
#!/usr/bin/env python
# -*- coding=utf-8 -*-
from application import create_app
__author__ = 'Riky'
application = create_app('idc')
if __name__ == "__main__":
application.run()
曾经蜡笔没有小新2017-05-16 17:19:52
uwsgi has a configuration file in which the access address and port number can be defined. I think the socket.error: [Errno 98] Address already in use sentence may be the reason why you have not configured uwsgi. For the configuration of uwsgi and the deployment process of the flask project, you can refer to the article I wrote, hehe.
/a/1190000004294...
黄舟2017-05-16 17:19:52
netstat -ntlp Check whether the port is occupied by uwsgi. If yes, ps -ef | grep uwsgi finds the pid and then kill