summaryrefslogtreecommitdiff
path: root/django/utils/asyncio.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/utils/asyncio.py')
-rw-r--r--django/utils/asyncio.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/django/utils/asyncio.py b/django/utils/asyncio.py
index c4de04ba12..2405e3413e 100644
--- a/django/utils/asyncio.py
+++ b/django/utils/asyncio.py
@@ -1,5 +1,6 @@
import asyncio
import functools
+import os
from django.core.exceptions import SynchronousOnlyOperation
@@ -12,14 +13,15 @@ def async_unsafe(message):
def decorator(func):
@functools.wraps(func)
def inner(*args, **kwargs):
- # Detect a running event loop in this thread.
- try:
- event_loop = asyncio.get_event_loop()
- except RuntimeError:
- pass
- else:
- if event_loop.is_running():
- raise SynchronousOnlyOperation(message)
+ if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):
+ # Detect a running event loop in this thread.
+ try:
+ event_loop = asyncio.get_event_loop()
+ except RuntimeError:
+ pass
+ else:
+ if event_loop.is_running():
+ raise SynchronousOnlyOperation(message)
# Pass onwards.
return func(*args, **kwargs)
return inner