File tree Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -530,3 +530,27 @@ def test_uvloop_policy(self):
530
530
loop .close ()
531
531
finally :
532
532
asyncio .set_event_loop_policy (None )
533
+
534
+ @unittest .skipUnless (hasattr (asyncio , '_get_running_loop' ),
535
+ 'No asyncio._get_running_loop' )
536
+ def test_running_loop_within_a_loop (self ):
537
+ @asyncio .coroutine
538
+ def runner (loop ):
539
+ loop .run_forever ()
540
+
541
+ try :
542
+ asyncio .set_event_loop_policy (uvloop .EventLoopPolicy ())
543
+
544
+ loop = asyncio .new_event_loop ()
545
+ outer_loop = asyncio .new_event_loop ()
546
+
547
+ try :
548
+ with self .assertRaisesRegex (RuntimeError ,
549
+ 'while another loop is running' ):
550
+ outer_loop .run_until_complete (runner (loop ))
551
+ finally :
552
+ loop .close ()
553
+ outer_loop .close ()
554
+
555
+ finally :
556
+ asyncio .set_event_loop_policy (None )
Original file line number Diff line number Diff line change @@ -38,6 +38,8 @@ cdef aio_Protocol = asyncio.Protocol
38
38
cdef aio_SSLProtocol = asyncio.sslproto.SSLProtocol
39
39
cdef aio_debug_wrapper = asyncio.coroutines.debug_wrapper
40
40
cdef aio_isfuture = getattr (asyncio, ' isfuture' , None )
41
+ cdef aio_get_running_loop = getattr (asyncio, ' _get_running_loop' , None )
42
+ cdef aio_set_running_loop = getattr (asyncio, ' _set_running_loop' , None )
41
43
42
44
cdef col_deque = collections.deque
43
45
cdef col_Iterable = collections.Iterable
Original file line number Diff line number Diff line change @@ -322,7 +322,12 @@ cdef class Loop:
322
322
raise RuntimeError (' unable to start the loop; it was closed' )
323
323
324
324
if self ._running == 1 :
325
- raise RuntimeError (' Event loop is running.' )
325
+ raise RuntimeError (' this event loop is already running.' )
326
+
327
+ if (aio_get_running_loop is not None and
328
+ aio_get_running_loop() is not None ):
329
+ raise RuntimeError (
330
+ ' Cannot run the event loop while another loop is running' )
326
331
327
332
if self ._signal_handlers is None :
328
333
self ._setup_signals()
@@ -337,7 +342,13 @@ cdef class Loop:
337
342
self .handler_check__exec_writes.start()
338
343
self .handler_idle.start()
339
344
340
- self .__run(mode)
345
+ if aio_set_running_loop is not None :
346
+ aio_set_running_loop(self )
347
+ try :
348
+ self .__run(mode)
349
+ finally :
350
+ if aio_set_running_loop is not None :
351
+ aio_set_running_loop(None )
341
352
342
353
self .handler_check__exec_writes.stop()
343
354
self .handler_idle.stop()
You can’t perform that action at this time.
0 commit comments