jonra1993 / fastapi-alembic-sqlmodel-async

This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
MIT License
878 stars 142 forks source link

Exception "got Future <Future pending> attached to a different loop" #94

Closed jcam5826 closed 3 months ago

jcam5826 commented 3 months ago

In the celery_task.py file you have this ...

@celery.task(name="tasks.print_hero")
def print_hero(hero_id: UUID) -> None:
    hero = runnify(get_hero)(hero_id=hero_id)
    return hero.id

I found an issue in implementing a version of the above code in my project. After a task is run once, I was getting a cryptic exception "got Future attached to a different loop". After a lot of head scratching, the following resolved the problem.

    asyncio.get_event_loop().run_until_complete(get_hero(hero_id=hero_id))

Sorry, I have not taken the time to confirm that your project has the same problem. I'm posting just in case someone else runs into the problem.

Thank you for your excellent project. It has helped a lot.

jonra1993 commented 3 months ago

Hello @jcam5826 I am sorry for the late reply and thanks for your positive feedback. Sure I am going to check this bug you had in your custom project and replicate it.

jonra1993 commented 3 months ago

Hello, @jcam5826 I was able to replicate the bug and I have updated the code as you suggest here using asyncio.get_event_loop().run_until_complete() https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/commit/0378bdbe20bfdcc7cf3066e72775d13af452a933