一行 app.run() 就能把应用跑起来,但它到底启动了什么?监听哪里?debug 多了哪些东西?这篇起真实进程,看日志、看进程、看监听端口。
app.py:
1 | import os, time, threading
2 | from flask import Flask
3 | app = Flask(__name__)
4 | @app.get("/")
5 | def index(): return f"pid={os.getpid()} run_main={os.environ.get('WERKZEUG_RUN_MAIN')}\n"
6 | @app.get("/slow")
7 | def slow():
8 | time.sleep(0.5); return "slow done\n"
9 | if __name__ == "__main__":
10 | app.run(port=int(os.environ["PORT"]), debug=os.environ.get("DBG") == "1", host=os.environ.get("HOST", "127.0.0.1"), threaded=os.environ.get("THR", "1") == "1")
GET / → 200 pid=30945 run_main=None
启动时服务器打印的内容(app.run 往 stderr 写):
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:9601
Press CTRL+C to quit
127.0.0.1 - - [02/Oct/2026 11:00:43] "GET / HTTP/1.1" 200 -
监听地址: ['127.0.0.1:9601']
相关进程数 1:
30945 30944 python@3.14/3.14.7/Frameworks/Python.framework/Versions/3.14/Resources/Python.app/Conten GET / → pid=30955 run_main=true
相关进程数 2:
30952 30944 python@3.14/3.14.7/Frameworks/Python.framework/Versions/3.14/Resources/Python.app/Conten
30955 30952 python@3.14/3.14.7/Frameworks/Python.framework/Versions/3.14/Resources/Python.app/Conten
启动信息:
* Serving Flask app 'app'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:9602
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: ***-***-***
127.0.0.1 - - [02/Oct/2026 11:00:44] "GET / HTTP/1.1" 200 -
改动 app.py 后 0.64 秒日志出现重载;再请求:pid=30980 run_main=true
重载相关日志:
* Restarting with stat
* Debugger is active!
* Debugger PIN: ***-***-***
* Detected change in '<实验目录>/fltmp/f2/app.py', reloading
…(共 7 行)host=127.0.0.1 → lsof 看到的监听:['127.0.0.1:9603'] host=0.0.0.0 → lsof 看到的监听:['*:9604']
threaded=True 总耗时 505, 513, 511 ms(中位数 511) threaded=False 总耗时 1014, 1023, 1018 ms(中位数 1018)
flask --app app run --port 9607 → GET / 200 pid=31032 run_main=None
启动信息:
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:9607
Press CTRL+C to quit
127.0.0.1 - - [02/Oct/2026 11:00:52] "GET / HTTP/1.1" 200 -
加 --debug:
* Debug mode: on
* Debugger is active!
* Debugger PIN: ***-***-***