This repository was archived by the owner on Nov 23, 2017. It is now read-only.
File tree 5 files changed +57
-4
lines changed
5 files changed +57
-4
lines changed Original file line number Diff line number Diff line change 41
41
streams .__all__ +
42
42
subprocess .__all__ +
43
43
tasks .__all__ +
44
- transports .__all__ )
44
+ transports .__all__ +
45
+ ['run' , 'forever' ]) # Will fix this later.
45
46
46
47
if sys .platform == 'win32' : # pragma: no cover
47
48
from .windows_events import *
Original file line number Diff line number Diff line change @@ -294,6 +294,9 @@ def __init__(self):
294
294
# Set to True when `loop.shutdown_asyncgens` is called.
295
295
self ._asyncgens_shutdown_called = False
296
296
297
+ # Future that isn't resolved while the loop is running.
298
+ self ._forever_fut = None
299
+
297
300
def __repr__ (self ):
298
301
return ('<%s running=%s closed=%s debug=%s>'
299
302
% (self .__class__ .__name__ , self .is_running (),
@@ -430,8 +433,12 @@ def shutdown_asyncgens(self):
430
433
'asyncgen' : agen
431
434
})
432
435
436
+ def get_forever_future (self ):
437
+ return self ._forever_fut
438
+
433
439
def run_forever (self ):
434
440
"""Run until stop() is called."""
441
+ self ._forever_fut = self .create_future ()
435
442
self ._check_closed ()
436
443
if self .is_running ():
437
444
raise RuntimeError ('This event loop is already running' )
@@ -450,7 +457,14 @@ def run_forever(self):
450
457
self ._run_once ()
451
458
if self ._stopping :
452
459
break
460
+ except BaseException as ex :
461
+ self ._forever_fut .set_exception (ex )
462
+ self ._forever_fut ._log_traceback = False
463
+ raise ex
464
+ else :
465
+ self ._forever_fut .set_result (None )
453
466
finally :
467
+ self ._forever_fut = None
454
468
self ._stopping = False
455
469
self ._thread_id = None
456
470
events ._set_running_loop (None )
Original file line number Diff line number Diff line change @@ -512,6 +512,9 @@ def get_debug(self):
512
512
def set_debug (self , enabled ):
513
513
raise NotImplementedError
514
514
515
+ def get_forever_future (self ):
516
+ raise NotImplementedError
517
+
515
518
516
519
class AbstractEventLoopPolicy :
517
520
"""Abstract policy for accessing the event loop."""
Original file line number Diff line number Diff line change 1
1
"""asyncio.run() function."""
2
2
3
- __all__ = ['run' ]
3
+ __all__ = ['run' , 'forever' ]
4
4
5
5
import threading
6
6
7
7
from . import coroutines
8
8
from . import events
9
9
10
10
11
+ @coroutines .coroutine
12
+ def forever ():
13
+ """Wait until the current event loop stops running.
14
+
15
+ The coroutine will return None if the loop is stopped by
16
+ calling the `loop.stop()` method.
17
+
18
+ The coroutine will propagate any exception that caused
19
+ the loop to stop;
20
+
21
+ It is recommended to use this coroutine with the asyncio.run()
22
+ function:
23
+
24
+ async def coro():
25
+ print('hi')
26
+ try:
27
+ await asyncio.forever()
28
+ except KeyboardInterrupt:
29
+ await asyncio.sleep(1)
30
+ print('bye')
31
+
32
+ asyncio.run(coro())
33
+ """
34
+ loop = events .get_event_loop ()
35
+ return (yield from loop .get_forever_future ())
36
+
37
+
11
38
def run (coro , * , debug = False ):
12
39
"""Run a coroutine.
13
40
@@ -50,7 +77,15 @@ async def main():
50
77
if debug :
51
78
loop .set_debug (True )
52
79
53
- result = loop .run_until_complete (coro )
80
+ task = loop .create_task (coro )
81
+ task .add_done_callback (lambda task : loop .stop ())
82
+
83
+ try :
84
+ loop .run_forever ()
85
+ except BaseException as ex :
86
+ result = loop .run_until_complete (task )
87
+ else :
88
+ result = task .result ()
54
89
55
90
try :
56
91
# `shutdown_asyncgens` was added in Python 3.6; not all
Original file line number Diff line number Diff line change @@ -302,7 +302,7 @@ def _step(self, exc=None):
302
302
def _wakeup (self , future ):
303
303
try :
304
304
future .result ()
305
- except Exception as exc :
305
+ except BaseException as exc :
306
306
# This may also be a cancellation.
307
307
self ._step (exc )
308
308
else :
You can’t perform that action at this time.
0 commit comments