multiprocessing and SIGINT

akineko akineko at gmail.com
Fri Jan 9 00:04:58 EST 2009


It is a bit awkward to respond to my posting but I have some updates.

I created two simple test programs, one uses threading and another
uses multiprocessing.

What I found was:
(1) test program with threading
Only main thread receives SIGINT (generated thread won't receive
SIGINT)
(2) test program with multiprocessing
Both processes receives SIGINT.
OS apparently distributes the SIGINT event to processes associated
with the terminal.
(3) signal handler
I realized that I could assign a signal handler specific to a process
by placing it to a worker method.

def worker():
   # this process ignores SIGINT
   signal.signal(signal.SIGINT, signal.SIG_IGN)
   ... the rest ...

def main():
   process = multiprocessing.Process(target=worker)
   process.start()

(4) terminating the spawned process
I needed to send a shutdown message to the process via a communication
between two processes.
You can use Process.terminate() to brutally kill the process but that
is a last resort.

Now my program can shutdown elegantly when I type Ctrl-C.
I see a big potential in multiprocessing as more PCs now have multi-
Core CPU.

Aki-



More information about the Python-list mailing list