05bit / peewee-async

Asynchronous interface for peewee ORM powered by asyncio
http://peewee-async-lib.readthedocs.io
MIT License
716 stars 98 forks source link

Dont work atomic() in fastapi task #215

Open g-ruby opened 2 months ago

g-ruby commented 2 months ago
_GatheringFuture exception was never retrieved
future: <_GatheringFuture finished exception=RuntimeError('The transaction must run within a task')>
Traceback (most recent call last):
  File "C:\Users\User\Desktop\git\microservice-users\routes\roles.py", line 160, in m
    async with objects.atomic():
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\peewee_async.py", line 1273, in __aenter__
    return (await self._ctx.__aenter__())
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\peewee_async.py", line 1202, in __aenter__
    raise RuntimeError("The transaction must run within a task")
RuntimeError: The transaction must run within a task
@router.patch("/roles")
@get_scopes('manage_roles')
async def role_patch(request: Request):

/// other code

    async def m():
        async with objects.atomic():
            for change in data_json:
                role_id = change.get("role_id")
                position = change.get("position")

                if role_id not in jpath.search("*.role_id", roles):
                    raise HTTPException(400, "The role you have in JSON was not found")

                if position not in range(1, len(roles)+1):
                    raise HTTPException(400, "Invalid positon role")

                role = await get_role(str(role_id))

                role.priority = 7 # value for example

                await objects.update(role)

    task = asyncio.create_task(m())
    asyncio.gather(task)
rudyryk commented 4 weeks ago

@kalombos Hi! Btw, the new aio_atomic() is in place, right? Maybe let's release it?

kalombos commented 4 weeks ago

Hi @rudyryk It is already released as beta and i use the version in my projects. I am going to make a prod-release version after the beta has been in production for a while