【问题标题】:why do i have error "Address already in use"?为什么我有错误“地址已在使用中”?
【发布时间】:2015-09-03 15:18:14
【问题描述】:

我运行我的烧瓶应用程序,它运行良好,但是当应用程序停止并在我的 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 touch_reload 时,应用程序再次运行。 我在服务器上运行任何其他可能占用套接字的东西。

我的会议:

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端口已经在使用中。
  • 你是如何停止进程的?
  • 您可能必须使用 ctrl+z 来停止该过程,但这实际上只是隐藏它。使用 ctrl+c 完全停止
  • 另外你可以使用 ps aux | grep 9002 查看 9002 用于什么
  • 我没有停止服务器,它正在生产

标签: python sockets nginx flask uwsgi


【解决方案1】:

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

sudo fuser -k 9002/tcp

如果那是一个 tcp 进程并重新启动您的 wsgi 服务器以查看该端口是否已在使用中。

【讨论】:

    【解决方案2】:

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

    1. 找到占用8000的进程的pid

    $ lsof -i:8000

    结果可能是:

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

    然后

    $杀死9196

    【讨论】:

    • 使用 -9 也可能有用(必要):` kill -9 9196`
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      所以请确保app.run()if __name__ == '__main__' 部分中。

      【讨论】:

        【解决方案5】:

        FWIW,我遇到了类似的问题,发现我在运行 uwsgi --http,而我应该运行 uwsgi --socket

        【讨论】:

          【解决方案6】:

          有趣的是,即使您使用套接字,您也会得到相同的 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"
          

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

          【讨论】:

            猜你喜欢
            • 2023-02-23
            • 1970-01-01
            • 1970-01-01
            • 2018-05-20
            • 1970-01-01
            • 2015-06-04
            • 2011-01-09
            • 2020-06-13
            • 1970-01-01
            相关资源
            最近更新 更多