1
0
mirror of https://github.com/django/django.git synced 2025-06-05 11:39:13 +00:00

Refs #31224 -- Made sync_to_async() examples use thread sensitive with ORM calls.

This commit is contained in:
Mariusz Felisiak 2020-09-03 11:33:23 +02:00 committed by Carlton Gibson
parent 0b8871ab67
commit 8d59075184

View File

@ -71,17 +71,18 @@ you will need to wrap it in a :func:`sync_to_async` call. For example::
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
results = sync_to_async(Blog.objects.get)(pk=123) results = await sync_to_async(Blog.objects.get, thread_sensitive=True)(pk=123)
You may find it easier to move any ORM code into its own function and call that You may find it easier to move any ORM code into its own function and call that
entire function using :func:`sync_to_async`. For example:: entire function using :func:`sync_to_async`. For example::
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
@sync_to_async def _get_blog(pk):
def get_blog(pk):
return Blog.objects.select_related('author').get(pk=pk) return Blog.objects.select_related('author').get(pk=pk)
get_blog = sync_to_async(_get_blog, thread_sensitive=True)
If you accidentally try to call a part of Django that is still synchronous-only If you accidentally try to call a part of Django that is still synchronous-only
from an async view, you will trigger Django's from an async view, you will trigger Django's
:ref:`asynchronous safety protection <async-safety>` to protect your data from :ref:`asynchronous safety protection <async-safety>` to protect your data from