1
0
mirror of synced 2025-01-19 06:27:23 +01:00

Better multi-threaded Ctrl+C handling.

This commit is contained in:
Jennifer Taylor 2021-05-16 19:39:44 +00:00
parent 604b987f90
commit 89af7c100a

View File

@ -1,6 +1,7 @@
import multiprocessing import multiprocessing
import signal
from PIL import Image # type: ignore from PIL import Image # type: ignore
from typing import List, Sequence, Tuple from typing import Any, List, Sequence, Tuple
from .types.generic import Color, Matrix, Point from .types.generic import Color, Matrix, Point
@ -225,6 +226,14 @@ def affine_composite(
work: multiprocessing.Queue = multiprocessing.Queue() work: multiprocessing.Queue = multiprocessing.Queue()
results: multiprocessing.Queue = multiprocessing.Queue() results: multiprocessing.Queue = multiprocessing.Queue()
expected: int = 0 expected: int = 0
interrupted: bool = False
def ctrlc(sig: Any, frame: Any) -> None:
nonlocal interrupted
interrupted = True
original_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, ctrlc)
for _ in range(cores): for _ in range(cores):
proc = multiprocessing.Process( proc = multiprocessing.Process(
@ -270,6 +279,10 @@ def affine_composite(
for proc in procs: for proc in procs:
proc.join() proc.join()
signal.signal(signal.SIGINT, original_handler)
if interrupted:
raise KeyboardInterrupt()
img = Image.frombytes('RGBA', (imgwidth, imgheight), b''.join(lines)) img = Image.frombytes('RGBA', (imgwidth, imgheight), b''.join(lines))
return img return img