Terminating a subprocess with KeyboardInterrupt
<p>I'm using Python to call a C++ program using the subprocess module. Since the program takes some time to run, I'd like to be able to terminate it using Ctrl+C. I've seen a few questions regarding this on StackOverflow but none of the solutions seem to work for me.</p>

<p>What I would like is for the subprocess to be terminated on KeyboardInterrupt. This is the code that I have (similar to suggestions in other questions):</p>

<pre><code>import subprocess

binary_path = '/path/to/binary'
args = 'arguments' # arbitrary

call_str = '{} {}'.format(binary_path, args)

proc = subprocess.Popen(call_str)

try:
    proc.wait()
except KeyboardInterrupt:
    proc.terminate()
</code></pre>

<p>However, if I run this, the code is hung up waiting for the process to end and never registers the KeyboardInterrupt. I have tried the following as well:</p>

<pre><code>import subprocess
import time

binary_path = '/path/to/binary'
args = 'arguments' # arbitrary

call_str = '{} {}'.format(binary_path, args)

proc = subprocess.Popen(call_str)
time.sleep(5)
proc.terminate()
</code></pre>

<p>This code snippet works fine at terminating the program, so it's not the actual signal that's being sent to terminate that is the problem.</p>

<p>How can I change the code so that the subprocess can be terminated on KeyboardInterrupt?</p>

<p>I'm running Python 2.7 and Windows 7 64-bit. Thanks in advance!</p>

<p>Some related questions that I tried:</p>

<p><a href="http://stackoverflow.com/questions/19807134/python-sub-process-ctrlc">Python sub process Ctrl+C</a></p>

<p><a href="http://stackoverflow.com/questions/33496416/kill-subprocess-call-after-keyboardinterrupt">Kill subprocess.call after KeyboardInterrupt</a></p>

<p><a href="http://stackoverflow.com/questions/1603658/kill-subprocess-when-python-process-is-killed">kill subprocess when python process is killed?</a></p>

<p>My ugly but successfull attempt on Windows:</p>

<pre><code>import subprocess
import threading

import time

binary_path = 'notepad'
args = 'foo.txt' # arbitrary

proc = None
done = False

def s():
    call_str = '{} {}'.format(binary_path, args)
    global done
    global proc
    proc = subprocess.Popen(call_str,stdout=subprocess.PIPE)
    proc.wait()
    done = True


t = threading.Thread(target=s)
t.start()


try:
    while not done:
        time.sleep(0.1)

except KeyboardInterrupt:
    print("terminated")
    proc.terminate()
</code></pre>

<p>Create a thread where the subprocess runs. Export the <code>proc</code> variable.</p>

<p>Then wait forever in a non-active loop. When CTRL+C is pressed, the exception is triggered. Inter-process communication (ex: <code>proc.wait()</code>) is conflicting with CTRL+C handling otherwise. When running in a thread, no such issue.</p>

<p>note: I have tried to avoid this time loop using <code>threading.lock()</code> but stumbled on the same CTRL+C ignore.</p>

python windows subprocess interrupt terminate