Skip to content

Commit 3d54452

Browse files
committed
Create missing loop in get_event_loop in main thread
This fixes issue #702.
1 parent 5910a18 commit 3d54452

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

uvloop/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,33 @@ def get_event_loop(self) -> _AbstractEventLoop:
202202
203203
Returns an instance of EventLoop or raises an exception.
204204
"""
205+
if (
206+
self._local._loop is None
207+
and threading.current_thread() is threading.main_thread()
208+
):
209+
# Replicate the behavior of asyncio.get_event_loop() as closely
210+
# as possible, including the warning and stack level.
211+
stacklevel = 2
212+
try:
213+
f = _sys._getframe(1)
214+
except AttributeError:
215+
pass
216+
else:
217+
# Move up the call stack so that the warning is attached
218+
# to the line outside uvloop itself.
219+
while f:
220+
module = f.f_globals['__name__']
221+
if not (module == 'uvloop' or module.startswith('uvloop.')):
222+
break
223+
f = f.f_back
224+
stacklevel += 1
225+
_warnings.warn(
226+
'There is no current event loop',
227+
DeprecationWarning,
228+
stacklevel=stacklevel
229+
)
230+
self._local._loop = self._loop_factory()
231+
205232
if self._local._loop is None:
206233
raise RuntimeError(
207234
'There is no current event loop in thread %r.'

0 commit comments

Comments
 (0)