44import signal
55import threading
66
7+
78class Runner :
89 """Runner is a class similar to asyncio.Runner but that we use for backward
910 compatibility with Python 3.10 and earlier.
@@ -24,7 +25,7 @@ def close(self):
2425 loop = self ._loop
2526 _cancel_all_tasks (loop )
2627 loop .run_until_complete (loop .shutdown_asyncgens ())
27- if hasattr (loop , ' shutdown_default_executor' ): # Python 3.9+
28+ if hasattr (loop , " shutdown_default_executor" ): # Python 3.9+
2829 loop .run_until_complete (loop .shutdown_default_executor ())
2930 finally :
3031 loop .close ()
@@ -41,12 +42,15 @@ def run(self, coro):
4142 except RuntimeError :
4243 pass
4344 else :
44- raise RuntimeError ("Runner.run() cannot be called from a running event loop" )
45+ raise RuntimeError (
46+ "Runner.run() cannot be called from a running event loop"
47+ )
4548
4649 task = self ._loop .create_task (coro )
4750 sigint_handler = None
4851
49- if (threading .current_thread () is threading .main_thread ()
52+ if (
53+ threading .current_thread () is threading .main_thread ()
5054 and signal .getsignal (signal .SIGINT ) is signal .default_int_handler
5155 ):
5256 sigint_handler = functools .partial (self ._on_sigint , main_task = task )
@@ -70,7 +74,8 @@ def run(self, coro):
7074 raise # CancelledError
7175 finally :
7276 asyncio .set_event_loop (None )
73- if (sigint_handler is not None
77+ if (
78+ sigint_handler is not None
7479 and signal .getsignal (signal .SIGINT ) is sigint_handler
7580 ):
7681 signal .signal (signal .SIGINT , signal .default_int_handler )
@@ -84,6 +89,7 @@ def _on_sigint(self, signum, frame, main_task):
8489 return
8590 raise KeyboardInterrupt ()
8691
92+
8793def _cancel_all_tasks (loop ):
8894 to_cancel = asyncio .all_tasks (loop )
8995 if not to_cancel :
@@ -98,8 +104,10 @@ def _cancel_all_tasks(loop):
98104 if task .cancelled ():
99105 continue
100106 if task .exception () is not None :
101- loop .call_exception_handler ({
102- 'message' : 'unhandled exception during asyncio.run() shutdown' ,
103- 'exception' : task .exception (),
104- 'task' : task ,
105- })
107+ loop .call_exception_handler (
108+ {
109+ "message" : "unhandled exception during asyncio.run() shutdown" ,
110+ "exception" : task .exception (),
111+ "task" : task ,
112+ }
113+ )
0 commit comments