Skip to content

Genorators using database_sync_to_async are called on the main thread #38

Open
@agronick

Description

@agronick

This is probably true for the other decorators as well. Same issue if you call the decorators directly.

threading.get_ident() is the same as the async code when the function uses yield. If you change it to return a list it has a different ID.

Heres the code

    @sync_to_async
    def function_foo(self):
        print(threading.get_ident(), 'function')
        return 4

    @sync_to_async
    def generator_foo(self):
        print(threading.get_ident(), 'generator')
        yield 4

    async def receive_json(self, content):
        print(threading.get_ident(), 'loop')
        print(await self.function_foo())
        for i in await self.generator_foo():
            print(i)

Heres the output


139945948370688 loop
139945849284352 function
4
139945948370688 generator
4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions