为什么我有错误 "Address already in use"?

why do i have error "Address already in use"?

我 运行 我的烧瓶应用程序,它运行良好,但是当应用程序停止并在我的 uwsgi 日志中时

probably another instance of uWSGI is running on the same address (127.0.0.1:9002).
    bind(): Address already in use [core/socket.c line 764]

当我 运行 触摸 touch_reload 时,应用程序再次运行。 我 运行 服务器上可能占用套接字的任何其他内容。

我的会议:[​​=13=]

nginx
server {
    listen 80;
    ....
    location / {
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:9001;
    }
    ....
}
server {
    listen 80;
    ....
    location / {
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:9003;
    }
    ....
}

uwsgi:
chdir = /var/www/../
module = wsgihandler
socket = 127.0.0.1:9003
wsgi-file = app/__init__.py
callable = app
master = true
chmod-socket = 664
uid = root
gid = root
processes = 4
socket-timeout = 180
post-buffering = 8192
max-requests = 1000
buffer-size = 32768
logto = /var/www/.../log/uwsgi.log
touch-reload = /var/www/.../touch_reload

此错误表示端口 9002 已被另一个进程占用。根据您的日志,该进程是 uwsgi probably another instance of uWSGI is running on the same address (127.0.0.1:9002)。可能是在您停止 flask 应用程序时端口未释放,并且在您 运行 touch touch_reload 时重新启动 wsgi 服务器。您可以尝试以下命令来释放端口。

sudo fuser -k 9002/tcp

如果这是一个 tcp 进程,请再次重启您的 wsgi 服务器以查看该端口是否已被使用。

我有同样的问题,但问题出在 sqlalchemy 中,尝试添加:

@app.teardown_request
def shutdown_session(exception=None):
    from extension import db
    db.session.remove()

我遇到了同样的问题,结果我的主模块已经在加载 app.run() 的情况下启动应用程序。

因此请确保 app.run()if __name__ == '__main__' 部分。

也许你通过 crtl + z 停止 uwsgi:

  1. 找到占用8000的进程pid

$ lsof -i:8000

结果可能是:

COMMAND  PID       USER   FD   TYPE ...
uwsgi   9196       xxx    4u  xxx   ...

然后

$ kill 9196

FWIW,我遇到了类似的问题,发现我本应 运行 uwsgi --socket.

运行 uwsgi --http

有趣的是,即使您使用套接字,您也会遇到相同的 Address already in use 错误。

               vvvvvvvvvvv-- Socket!
error removing unix socket, unlink(): Permission denied [core/socket.c line 198]
bind(): Address already in use [core/socket.c line 230]
        ^^^^^^^^^^^^^^^-- "Address"

如果您确实使用套接字,请参阅