align API debug launch with gevent startup

This commit is contained in:
hjlarry 2026-04-14 15:40:13 +08:00
parent 750f2edda7
commit 6252bab8d2
3 changed files with 23 additions and 27 deletions

View File

@ -2,21 +2,10 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Python: Flask API", "name": "Python: API (gevent)",
"type": "debugpy", "type": "debugpy",
"request": "launch", "request": "launch",
"module": "flask", "program": "${workspaceFolder}/api/app.py",
"env": {
"FLASK_APP": "app.py",
"FLASK_ENV": "development"
},
"args": [
"run",
"--host=0.0.0.0",
"--port=5001",
"--no-debugger",
"--no-reload"
],
"jinja": true, "jinja": true,
"justMyCode": true, "justMyCode": true,
"cwd": "${workspaceFolder}/api", "cwd": "${workspaceFolder}/api",

View File

@ -3,29 +3,21 @@
"compounds": [ "compounds": [
{ {
"name": "Launch Flask and Celery", "name": "Launch Flask and Celery",
"configurations": ["Python: Flask", "Python: Celery"] "configurations": ["Python: API (gevent)", "Python: Celery"]
} }
], ],
"configurations": [ "configurations": [
{ {
"name": "Python: Flask", "name": "Python: API (gevent)",
"consoleName": "Flask", "consoleName": "API",
"type": "debugpy", "type": "debugpy",
"request": "launch", "request": "launch",
"python": "${workspaceFolder}/.venv/bin/python", "python": "${workspaceFolder}/.venv/bin/python",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"envFile": ".env", "envFile": ".env",
"module": "flask", "program": "${workspaceFolder}/app.py",
"justMyCode": true, "justMyCode": true,
"jinja": true, "jinja": true
"env": {
"FLASK_APP": "app.py",
"GEVENT_SUPPORT": "True"
},
"args": [
"run",
"--port=5001"
]
}, },
{ {
"name": "Python: Celery", "name": "Python: Celery",

View File

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import logging
import sys import sys
from typing import TYPE_CHECKING, cast from typing import TYPE_CHECKING, cast
@ -9,12 +10,25 @@ if TYPE_CHECKING:
celery: Celery celery: Celery
HOST = "0.0.0.0"
PORT = 5001
logger = logging.getLogger(__name__)
def is_db_command() -> bool: def is_db_command() -> bool:
if len(sys.argv) > 1 and sys.argv[0].endswith("flask") and sys.argv[1] == "db": if len(sys.argv) > 1 and sys.argv[0].endswith("flask") and sys.argv[1] == "db":
return True return True
return False return False
def log_startup_banner(host: str, port: int) -> None:
debugger_attached = sys.gettrace() is not None
logger.info("Serving Dify API via gevent WebSocket server")
logger.info("Bound to http://%s:%s", host, port)
logger.info("Debugger attached: %s", "on" if debugger_attached else "off")
logger.info("Press CTRL+C to quit")
# create app # create app
flask_app = None flask_app = None
socketio_app = None socketio_app = None
@ -43,5 +57,6 @@ if __name__ == "__main__":
from gevent import pywsgi from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler # type: ignore[reportMissingTypeStubs] from geventwebsocket.handler import WebSocketHandler # type: ignore[reportMissingTypeStubs]
server = pywsgi.WSGIServer(("0.0.0.0", 5001), socketio_app, handler_class=WebSocketHandler) log_startup_banner(HOST, PORT)
server = pywsgi.WSGIServer((HOST, PORT), socketio_app, handler_class=WebSocketHandler)
server.serve_forever() server.serve_forever()