使用 FastAPI Python 开始。
以下是我构建服务器的方式。
class Server:
def __init__(self):
self.app = FastAPI()
def runServer(self, host: str, port: int,is_dev:bool):
uvicorn.run(self.app, host=host, port=port,debug=is_dev)
if __name__ == "__main__":
server = Server()
# read the environment variables
host: str = os.environ['host']
port: int = int(os.environ['port'])
is_dev: bool = bool(os.environ['dev'])
server.runServer(host, port, is_dev)
这将启动服务器,但如果我进行任何更改,则不会运行自动重新加载模式。
即使我尝试过
uvicorn.run(self.app, host=host, port=port, reload=is_dev)
重新加载(reload)可能不是一个选项,这导致服务器崩溃。
我尝试在launch.json中传递--reload
参数,但仍然无法工作。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: FastAPI",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py",
"jinja": true,
"args": ["--reload"],
"env": {
"host": "127.0.0.1",
"port": "5555",
"dev": "true"
}
}
]
}
有什么想法吗?我有什么遗漏的吗?
example:app
感到困惑:我假设(因为这个理论对我有效)example
是你正在运行 FastAPI 应用程序的文件名,因此在这种情况下是example.py
,而app
来自于app = FastAPI(debug=True)
。 - DangerPaws