summaryrefslogtreecommitdiff
path: root/django/db/utils.py
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2019-04-12 06:15:18 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-20 12:29:43 +0200
commita415ce70bef6d91036b00dd2c8544aed7aeeaaed (patch)
tree3583cef22e9b56d2ed52456ab586d9c47620bc51 /django/db/utils.py
parentcce47ff65a4dd3786c049ec14ee889e128ca7de9 (diff)
Fixed #30451 -- Added ASGI handler and coroutine-safety.
This adds an ASGI handler, asgi.py file for the default project layout, a few async utilities and adds async-safety to many parts of Django.
Diffstat (limited to 'django/db/utils.py')
-rw-r--r--django/db/utils.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/django/db/utils.py b/django/db/utils.py
index cb7f3d0f0b..4bd119227f 100644
--- a/django/db/utils.py
+++ b/django/db/utils.py
@@ -1,7 +1,8 @@
import pkgutil
from importlib import import_module
from pathlib import Path
-from threading import local
+
+from asgiref.local import Local
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
@@ -139,7 +140,12 @@ class ConnectionHandler:
like settings.DATABASES).
"""
self._databases = databases
- self._connections = local()
+ # Connections needs to still be an actual thread local, as it's truly
+ # thread-critical. Database backends should use @async_unsafe to protect
+ # their code from async contexts, but this will give those contexts
+ # separate connections in case it's needed as well. There's no cleanup
+ # after async contexts, though, so we don't allow that if we can help it.
+ self._connections = Local(thread_critical=True)
@cached_property
def databases(self):