KeyboardInterrupt should not kill subprocess

M

Michael Goerz

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
 
D

Donn Cave

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, (e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top