mirror of
https://github.com/django/django.git
synced 2025-07-03 17:29:12 +00:00
[3.2.x] Refs #32074 -- Used asyncio.get_running_loop() instead of get_event_loop() on Python 3.7+.
Using asyncio.get_event_loop() when there is no running event loop was deprecated in Python 3.10, see https://bugs.python.org/issue39529.
This commit is contained in:
parent
f6726fdc3e
commit
53fad80ffe
@ -3,6 +3,13 @@ import functools
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from django.core.exceptions import SynchronousOnlyOperation
|
from django.core.exceptions import SynchronousOnlyOperation
|
||||||
|
from django.utils.version import PY37
|
||||||
|
|
||||||
|
|
||||||
|
if PY37:
|
||||||
|
get_running_loop = asyncio.get_running_loop
|
||||||
|
else:
|
||||||
|
get_running_loop = asyncio.get_event_loop
|
||||||
|
|
||||||
|
|
||||||
def async_unsafe(message):
|
def async_unsafe(message):
|
||||||
@ -16,11 +23,11 @@ def async_unsafe(message):
|
|||||||
if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):
|
if not os.environ.get('DJANGO_ALLOW_ASYNC_UNSAFE'):
|
||||||
# Detect a running event loop in this thread.
|
# Detect a running event loop in this thread.
|
||||||
try:
|
try:
|
||||||
event_loop = asyncio.get_event_loop()
|
event_loop = get_running_loop()
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if event_loop.is_running():
|
if PY37 or event_loop.is_running():
|
||||||
raise SynchronousOnlyOperation(message)
|
raise SynchronousOnlyOperation(message)
|
||||||
# Pass onwards.
|
# Pass onwards.
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user