Browse Source

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

Mariusz Felisiak 4 years ago
parent
commit
8d59075184
1 changed files with 4 additions and 3 deletions
  1. 4 3
      docs/topics/async.txt

+ 4 - 3
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
     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