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

TaskLocals doesn't share parent's Task context with child one's #123

Open
mullakhmetov opened this issue Apr 18, 2019 · 0 comments
Open

Comments

@mullakhmetov
Copy link

Peewee related task context such as connection or transaction_depth is stored in TaskLocals object with id(asyncio.current_task()) acts like a key.

This behavior causes painful problem with asyncio.gather inside transaction block:

from models import TestModel

class MyTest(BaseTest):
    async def create_obj(self):
        return await self.objects.create(TestModel)

    async def get_obj(self, obj_id):
        await self.objects.get(TestModel, TestModel.id == obj_id)

    async def indirect_get_obj(self, obj_id):
        await self.get_obj(obj_id)

    async def ensured_get_obj(self, obj_id):
        await asyncio.ensure_future(self.get_obj(obj_id))
    
    async def test_transaction(self):
        async with self.objects.atomic():
            obj_id = (await self.create_obj()).id

            await self.objects.get(TestModel, TestModel.id == obj_id)
            await self.get_obj(obj_id)
            await self.indirect_get_obj(obj_id)

            with self.assertRaises(TestModel.DoesNotExist):
                # new tasks loose context and spawn new db connections
                await self.ensured_get_obj(obj_id)
                await asyncio.gather(self.get_obj(obj_id))
@mullakhmetov mullakhmetov changed the title TaskLocals doesn't share parent's Task context to child one's TaskLocals doesn't share parent's Task context with child one's Apr 24, 2019
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

1 participant