Port XXXX is in use by another program. Either identify and stop that program, or start the server w

当尝试在AppEngine上运行一个PythonFlaskHelloWorld应用时,遇到了端口4444(以及默认的5000端口)被占用的问题。日志显示应用可能在多个实例或线程中并发运行。作者通过使用lsof命令找出占用端口的进程PID,然后使用kill命令终止该进程,成功解决了问题。此方法适用于Mac和Linux用户,Windows用户可寻找lsof和kill命令的等效工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I want to run a single python flask hello world. I deploy to App Engine, but it’s showing like it’s saying that the port is in use and it looks like it’s running on multiple instances/threads/clones concurrently.

This is my main.py

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def helloIndex():
    print("Hello world log console")
    return 'Hello World from Python Flask!'
      
app.run(host='0.0.0.0', port=4444)

And this is the logs that I got:

* Serving Flask app 'main'
* Debug mode: off
Address already in use
Port 4444 is in use by another program. Either identify and stop that program, or start the server with a different port.
[2022-08-10 15:57:28 +0000] [1058] [INFO] Worker exiting (pid: 1058)
[2022-08-10 15:57:29 +0000] [1059] [INFO] Booting worker with pid: 1059
[2022-08-10 15:57:29 +0000] [1060] [INFO] Booting worker with pid: 1060
[2022-08-10 15:57:29 +0000] [1061] [INFO] Booting worker with pid: 1061

It says that Port 4444 is in use. Initially I tried 5000 (flask’s default port) but it says it’s in use. Also I tried removing the port=4444 but now it’s saying Port 5000 is in use by another program, I guess flask by default assign port=5000. I’m suspecting that it’s because GAE is running in multiple instances that’s causing this error. If not, then please help to solve this issue.

I solved using lsof and kill commands. In my case, error message mentioned port 5000 so I first checked which processes(s) are using port 5000 by calling lsof -i:5000 in the terminal. Once I located the PID of already running Flask application, I killed it using this command: kill -9 and voilà!

This approach should work for both mac and Linux users. If Windows users can find the equivalent of the lsof and kill Linux commands, it may work there as well.

简单总结下:当运行程序显示某个端口被占用时,可以使用lsof 命令 -i:xxxx产看占用端口的程序,之后使用kill ,关闭程序即可。

lsof 安装:

apt-get install lsof
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值