[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

KeyboardInterrupt should not kill subprocess

Michael Goerz

2/21/2008 5:36:00 PM

Hi,

I'm using subprocess.Popen() to run some background processes. However,
the program is also supposed to catch CTRL+C keyboard interrupts for
refreshs (i.e. a keyboard interrupt doesn't shut down the program).

But as it seems, a keyboard interrupt will automatically pass down to
the subprocesses, causing them to abort. Is there a way that I can
prevent the subprocesses from being canceled by a keyboard interrupt?

To clarify my question, here is a minimal example:

import subprocess
import os
import time
import sys

# is 'sleep' available on Win machines?
# use another dummy program if it isn't
p = subprocess.Popen(['sleep', '10'])

while True:
try:
time.sleep(1)
pass # normal program procedure
print "subprocess poll: " + str(p.poll())
except KeyboardInterrupt:
try:
print("Hit Ctrl+C again to quit")
time.sleep(1)
print "Refreshing"
pass # do some refresh stuff here
except KeyboardInterrupt:
sys.exit(0)

As you can see, after the refresh, p.poll() is '-2'. I'd want the
subprocess to continue undisturbed, i.e. p.poll() would still return 'None'.


Thanks,
Michael
1 Answer

Donn Cave

2/22/2008 1:26:00 AM

0

In article <625r3oF21uf4hU1@mid.uni-berlin.de>,
Michael Goerz <newsgroup898sfie@8439.e4ward.com> wrote:


> But as it seems, a keyboard interrupt will automatically pass down to
> the subprocesses, causing them to abort. Is there a way that I can
> prevent the subprocesses from being canceled by a keyboard interrupt?


You might try posix.setsid(), from the child fork.

The object is to get the child fork out of the foreground
process group for the Berkeley terminal driver. This defines
who gets signals, when they originate in terminal control keys.

Donn Cave, donn@u.washington.edu