Why we need sync_to_async in Django? [duplicate]

The document said:

The reason this is needed in Django is that many libraries, specifically database adapters, require that they are accessed in the same thread that they were created in. Also a lot of existing Django code assumes it all runs in the same thread, e.g. middleware adding things to a request for later use in views.

But another question Is it safe that when Two asyncio tasks access the same awaitable object? said python's asyncio is thread safe.

And as I know since the GIL still exist accessing one object from multiple thread should be thread safe.

Can any one give a minimal example for why we have to use await sync_to_async(foo)() instead of directly foo() in django or other async apps?

Вернуться на верх