![](/img/trans.png)
[英]How to trigger lifespan startup and shutdown while testing FastAPI app?
[英]FastApi: App startup hangs at Router URL parsing
我正在编写一个带有简单路由器的 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",
)
您应该调用FastAPI
类的include_router(...)
--(FastAPI Doc)方法,而不是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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.