forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-126434: Continue process signals on dedicated thread, don't …
…bubble exception on main thread
- Loading branch information
Showing
5 changed files
with
99 additions
and
110 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import signal | ||
import threading | ||
import time | ||
import multiprocessing | ||
|
||
def sigint_self(): | ||
time.sleep(1) | ||
print(f'{threading.current_thread().name}: Stopping PID {os.getpid()} with SIGINT') | ||
os.kill(os.getpid(), signal.SIGINT) | ||
|
||
def sigkill_self(): | ||
time.sleep(5) | ||
print(f'{threading.current_thread().name}: Killing PID {os.getpid()} with SIGKILL') | ||
os.kill(os.getpid(), signal.SIGKILL) | ||
|
||
def run_signal_handler_dedicated_thread(): | ||
event = multiprocessing.Event() | ||
def sigint_handler(_signo, _stack_frame): | ||
try: | ||
x = 1 / 0 | ||
print(f'{threading.current_thread().name}: sigint_handler is setting event') | ||
event.set() | ||
finally: | ||
print(f'{threading.current_thread().name}: sigint_handler is done') | ||
|
||
def sigterm_handler(_signo, _stack_frame): | ||
print(f'{threading.current_thread().name}: sigterm_handler is running') | ||
pass | ||
|
||
signal.signal(signal.SIGTERM, sigterm_handler) | ||
signal.signal(signal.SIGINT, sigint_handler) | ||
|
||
threading.Thread(target=sigint_self, daemon=True).start() | ||
threading.Thread(target=sigkill_self, daemon=True).start() # Used for debugging only. | ||
|
||
print(f'{threading.current_thread().name}: Waiting on event. PID = {os.getpid()}') | ||
event.wait() | ||
print(f'{threading.current_thread().name}: Waiting is done') | ||
|
||
if __name__ == '__main__': | ||
try: | ||
run_signal_handler_dedicated_thread() | ||
finally: | ||
print(f'{threading.current_thread().name}: Exiting') |