我想运行一个 python flask hello world。我部署到 App Engine,但它显示端口正在使用中,看起来它同时在多个实例/线程/克隆上运行。
这是我的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)
这是我的 app.yaml
runtime: python38
env: standard
instance_class: B2
handlers:
- url: /
script: auto
- url: .*
script: auto
manual_scaling:
instances: 1
这是我的要求.txt
gunicorn==20.1.0
flask==2.2.2
这是我得到的日志:
* 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
它说端口 4444 正在使用中。最初我尝试了 5000(烧瓶的默认端口),但它说它正在使用中。我也尝试删除
port=4444
但现在它说Port 5000 is in use by another program
,我猜烧瓶默认分配端口= 5000。我怀疑是因为 GAE 在导致此错误的多个实例中运行。如果没有,那么请帮助解决这个问题。
我使用
lsof
和 kill
命令解决了。在我的例子中,错误消息提到了端口 5000,所以我首先通过在终端中调用 lsof -i:5000
来检查哪些进程正在使用端口 5000。一旦我找到已经运行的PID
应用程序的Flask
,我就使用这个命令杀死了它:kill -9 <PID>
和voilà!
这种方法应该适用于 mac 和 Linux 用户。如果 Windows 用户可以找到
lsof
和 kill
Linux 命令的等价物,它也可以在那里工作。
App Engine 应用程序应该监听端口 8080 而不是任何其他端口。
所以你可能需要这样设置
app.run(host='0.0.0.0', port=8080)
关闭编辑器然后重新打开。下次停止进程时,如果您在终端上,请使用 Ctrl+C
我想通了。从创建 Web 应用程序的终端和/或文件夹中删除旧文件。在终端中,这是通过以下方式完成的:
rm -file_name
然后用你的新文件再试一次,应该没问题。
lsof -i :5000
kill -9 PID