Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Jun 4, 2024
1 parent 09bd3e8 commit 324133f
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/dispatch/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import signal
import threading


class Runner:
"""Runner is a class similar to asyncio.Runner but that we use for backward
compatibility with Python 3.10 and earlier.
Expand All @@ -24,7 +25,7 @@ def close(self):
loop = self._loop
_cancel_all_tasks(loop)
loop.run_until_complete(loop.shutdown_asyncgens())
if hasattr(loop, 'shutdown_default_executor'): # Python 3.9+
if hasattr(loop, "shutdown_default_executor"): # Python 3.9+
loop.run_until_complete(loop.shutdown_default_executor())
finally:
loop.close()
Expand All @@ -41,12 +42,15 @@ def run(self, coro):
except RuntimeError:
pass
else:
raise RuntimeError("Runner.run() cannot be called from a running event loop")
raise RuntimeError(
"Runner.run() cannot be called from a running event loop"
)

task = self._loop.create_task(coro)
sigint_handler = None

if (threading.current_thread() is threading.main_thread()
if (
threading.current_thread() is threading.main_thread()
and signal.getsignal(signal.SIGINT) is signal.default_int_handler
):
sigint_handler = functools.partial(self._on_sigint, main_task=task)
Expand All @@ -70,7 +74,8 @@ def run(self, coro):
raise # CancelledError
finally:
asyncio.set_event_loop(None)
if (sigint_handler is not None
if (
sigint_handler is not None
and signal.getsignal(signal.SIGINT) is sigint_handler
):
signal.signal(signal.SIGINT, signal.default_int_handler)
Expand All @@ -84,6 +89,7 @@ def _on_sigint(self, signum, frame, main_task):
return
raise KeyboardInterrupt()


def _cancel_all_tasks(loop):
to_cancel = asyncio.all_tasks(loop)
if not to_cancel:
Expand All @@ -98,8 +104,10 @@ def _cancel_all_tasks(loop):
if task.cancelled():
continue
if task.exception() is not None:
loop.call_exception_handler({
'message': 'unhandled exception during asyncio.run() shutdown',
'exception': task.exception(),
'task': task,
})
loop.call_exception_handler(
{
"message": "unhandled exception during asyncio.run() shutdown",
"exception": task.exception(),
"task": task,
}
)

0 comments on commit 324133f

Please sign in to comment.