python - FastApi:应用程序启动在路由器 URL 解析时挂起
问题描述
我正在用简单的路由器编写一个 fastapi 应用程序,当我尝试启动应用程序时,启动会挂起在 re 模块中的 URL 解析。请在下面的 KeyboardInterrupt 上找到堆栈跟踪。
Traceback (most recent call last):
File "/Users/ssekar/ssa/code/classifier-api/app/main.py", line 7, in <module>
from app.api.classification_api import router as api_router
File "/Users/ssekar/ssa/code/classifier-api/app/api/classification_api.py", line 22, in <module>
router.include_router(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 569, in include_router
self.add_api_route(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 439, in add_api_route
route = route_class(
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/fastapi/routing.py", line 290, in __init__
self.path_regex, self.path_format, self.param_convertors = compile_path(path)
File "/Users/ssekar/Library/Caches/pypoetry/virtualenvs/classifier-api-MVO9j3br-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 123, in compile_path
return re.compile(path_regex), path_format, param_convertors
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/re.py", line 252, in compile
return _compile(pattern, flags)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/sre_parse.py", line 529, in _parse
subpatternappend((LITERAL, _ord(this)))
KeyboardInterrupt
编码
from fastapi import APIRouter
from starlette import status
from starlette.responses import JSONResponse
from app.services.metedata_services import get_categories
router = APIRouter()
@router.get(
"/categories",
status_code=status.HTTP_200_OK,
)
def categories(tree: str):
.....
return JSONResponse(content=response)
router.include_router(
router,
tags=["categories"],
prefix="/v1",
)
解决方案
您应该调用类的include_router(...)
--(FastAPI Doc)方法FastAPI
,而不是APIRouter
# some_app/api/routers.py
from fastapi import APIRouter
from starlette import status
from starlette.responses import JSONResponse
router = APIRouter()
@router.get(
"/categories",
status_code=status.HTTP_200_OK,
)
def categories(tree: str):
response = {"foo": "bar"}
return JSONResponse(content=response)
# main.py
import uvicorn
from fastapi import FastAPI
from some_app.api.routers import router
app = FastAPI()
app.include_router(
router,
tags=["categories"],
prefix="/v1",
)
if __name__ == "__main__":
uvicorn.run(app)
推荐阅读
- c# - 无法将文件附加为数据库
- keycloak - Keycloak 电子邮件验证不起作用并通过 keycloak 接收任何电子邮件
- django - Django 模型和查询集
- ssh - 如何在 Windows 10 上使用 C# 以编程方式检查 SSH 私钥是否具有密码短语
- android - ZXING Intent integrator in view
- apache-spark - Spark MLlib 仅在阈值大于值时进行预测
- c# - Log4net 不通过远程桌面登录
- ruby-on-rails - Rails 5,权威人士授权
- google-cloud-platform - 无法通过 REST Api 对谷歌数据存储进行身份验证
- dataset - R查询:向.txt文件添加标题行