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

被折叠的 条评论
为什么被折叠?



