diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-09-03 11:33:23 +0200 |
|---|---|---|
| committer | Carlton Gibson <carlton.gibson@noumenal.es> | 2020-09-03 12:07:25 +0200 |
| commit | f36c441f80c9c2cf69cbfa3254976aae08ef15f9 (patch) | |
| tree | 108a7c9aceadc14fe79d1f1b3eb05a78d3412a3a | |
| parent | 17d5b16dbf1ade22581850372b2b6f650a8e7e50 (diff) | |
[3.1.x] Refs #31224 -- Made sync_to_async() examples use thread sensitive with ORM calls.
Backport of 8d59075184f4cd2852b374359850ff7aeb412ec1 from master
| -rw-r--r-- | docs/topics/async.txt | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/docs/topics/async.txt b/docs/topics/async.txt index 528764ab2a..00a28e5a14 100644 --- a/docs/topics/async.txt +++ b/docs/topics/async.txt @@ -73,17 +73,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 |
