multiprocessing and SIGINT

A

akineko

Hello everyone,

I'm trying to use multiprocessing module, which is now available with
Python 2.6.
It is a nice enhancement and it worked great.

However, I have a situation and I couldn't figure out how to deal
with.

My Python program spawns another process to take care of GUI house-
keeping.
When I type Ctrl-C to my terminal, instead of my main process, the
spawned process always gets the SIGINT.

I don't want the spawned process to handle SIGINT event. An exception
handler (KeyboardInterrupt) is placed in the main program (but it
didn't catch the event).

I never encountered this problem before as spawned threads won't take
SIGINT event.

Is there any way I can force SIGINT event routing so that my main
process will get it?
Because of another requirement, I cannot swap main and spawned
process.

Any suggestions will be greatly appreciated.

Best regards,
Aki Niimura
 
A

akineko

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-
 
G

Gabriel Genellina

(2) test program with multiprocessing
Both processes receives SIGINT.
OS apparently distributes the SIGINT event to processes associated
with the terminal.

Yes, to avoid that, the child process has to detach itself from the
terminal. I'd expect the multiprocessing module to do that for us - but it
doesn't. See http://www.onlamp.com/python/pythoncook2/solution.csp?day=1
(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 ...

Looks fine...
(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.

What about sending a signal, like SIGTERM?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,849
Latest member
Fira

Latest Threads

Top