简体   繁体   中英

FastApi: App startup hangs at Router URL parsing

I'm writing a fastapi app with simple routers, when I try to start the app the start-up hangs at URL parsing in the re module. Please find the stack trace on KeyboardInterrupt below.

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

The code

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",
)

You should call the include_router(...) --(FastAPI Doc) method of FastAPI class, not 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

    tags=["categories"],
    prefix="/v1",
)

if __name__ == "__main__":
    uvicorn.run(app)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM