-
-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move wait_for_signals to private module and deprecate distributed.cli…
….utils
- Loading branch information
1 parent
5f503fb
commit 6cb42e5
Showing
4 changed files
with
34 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
import signal | ||
from typing import Any | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def wait_for_signals(signals: list[signal.Signals]) -> None: | ||
"""Wait for the passed signals by setting global signal handlers""" | ||
loop = asyncio.get_running_loop() | ||
event = asyncio.Event() | ||
|
||
old_handlers: dict[int, Any] = {} | ||
|
||
def handle_signal(signum, frame): | ||
# Restore old signal handler to allow for quicker exit | ||
# if the user sends the signal again. | ||
signal.signal(signum, old_handlers[signum]) | ||
logger.info("Received signal %s (%d)", signal.Signals(signum).name, signum) | ||
loop.call_soon_threadsafe(event.set) | ||
|
||
for sig in signals: | ||
old_handlers[sig] = signal.signal(sig, handle_signal) | ||
|
||
await event.wait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters