summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2020-09-03 11:33:23 +0200
committerCarlton Gibson <carlton@noumenal.es>2020-09-03 12:06:42 +0200
commit8d59075184f4cd2852b374359850ff7aeb412ec1 (patch)
tree73c5408ac4cc3adee208770828921865b4477726 /docs
parent0b8871ab6744285943784795ede053839ee009ef (diff)
Refs #31224 -- Made sync_to_async() examples use thread sensitive with ORM calls.
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/async.txt7
1 files changed, 4 insertions, 3 deletions
diff --git a/docs/topics/async.txt b/docs/topics/async.txt
index 5612235622..a2f702b8e3 100644
--- a/docs/topics/async.txt
+++ b/docs/topics/async.txt
@@ -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
- 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
entire function using :func:`sync_to_async`. For example::
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)
+ 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
from an async view, you will trigger Django's
:ref:`asynchronous safety protection <async-safety>` to protect your data from