Skip to content

Commit fadf6f7

Browse files
committed
pass the socket as the first arg allowing any thread to signal to exit
1 parent 11604aa commit fadf6f7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

async_utils/sig_service.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from typing import Any
2424

2525
type SignalCallback = Callable[[signal.Signals], Any]
26-
type StartStopCall = Callable[[], Any]
26+
type StartCall = Callable[[socket.socket], Any]
27+
type StopCall = Callable[[], Any]
2728
type _HANDLER = Callable[[int, FrameType | None], Any] | int | signal.Handlers | None
2829

2930
__all__ = ["SignalService"]
@@ -36,10 +37,10 @@ class SignalService:
3637
"""Meant for graceful signal handling where the main thread is only used for signal handling.
3738
This should be paired with event loops being run in threads."""
3839

39-
def __init__(self, *, startup: list[StartStopCall], signal_cbs: list[SignalCallback], joins: list[StartStopCall]) -> None:
40-
self._startup: list[StartStopCall] = startup
40+
def __init__(self, *, startup: list[StartCall], signal_cbs: list[SignalCallback], joins: list[StopCall]) -> None:
41+
self._startup: list[StartCall] = startup
4142
self._cbs: list[SignalCallback] = signal_cbs
42-
self._joins: list[StartStopCall] = joins
43+
self._joins: list[StopCall] = joins
4344

4445
def run(self):
4546
ss, cs = socket.socketpair()
@@ -56,7 +57,7 @@ def run(self):
5657
signal.siginterrupt(sig, False)
5758

5859
for task_start in self._startup:
59-
task_start()
60+
task_start(cs)
6061

6162
select.select([ss], [], [])
6263
data, *_ = ss.recv(4096)

0 commit comments

Comments
 (0)