tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.37k stars 355 forks source link

I integrated tortoise-orm in FastAPI, but it reported an error when started in debug mode #1663

Closed wesley-zen closed 1 week ago

wesley-zen commented 1 week ago

Describe the bug I integrated tortoise-orm in FastAPI, but it reported an error when started in debug mode

This is my project structure

this is my code: ` @asynccontextmanager async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:

app startup

async with RegisterTortoise(
        app,
        db_url=setting.dbUrl,
        modules={"models": ["dao.models"]},
        generate_schemas=True,
        add_exception_handlers=True,
) as session:
    # db connected
    yield
    # app teardown
    # db connections closed

app = FastAPI(lifespan=lifespan)

if name == "main": uvicorn.run(app, host="0.0.0.0", port=8080) ` this is error detail: Exception in callback <Task pending name='Task-1' coro=<Server.serve() running at /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/server.py:71> cb=[run_until_complete..done_cb()]>\nhandle: <Handle <Task pending name='Task-1' coro=<Server.serve() running at /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/server.py:71> cb=[run_until_complete..done_cb()]>>\nTraceback (most recent call last):\n File \"uvloop/cbhandles.pyx\", line 61, in uvloop.loop.Handle._run\nTypeError: 'Task' object is not callable