Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont work atomic() in fastapi task #215

Open
g-ruby opened this issue Apr 12, 2024 · 2 comments
Open

Dont work atomic() in fastapi task #215

g-ruby opened this issue Apr 12, 2024 · 2 comments

Comments

@g-ruby
Copy link

g-ruby commented Apr 12, 2024

_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
Copy link
Member

rudyryk commented Jun 8, 2024

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

@kalombos
Copy link
Collaborator

kalombos commented Jun 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants