How do I run a sub-process, display its output in a GUI and allow it to be terminated?
<p>I have been trying to write an application that runs subprocesses and (among other things) displays their output in a GUI and allows the user to click a button to cancel them. I start the processes like this:</p>

<pre><code>queue = Queue.Queue(500)
process = subprocess.Popen(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
iothread = threading.Thread(
    target=simple_io_thread,
    args=(process.stdout, queue))
iothread.daemon=True
iothread.start()
</code></pre>

<p>where simple_io_thread is defined as follows:</p>

<pre><code>def simple_io_thread(pipe, queue):
    while True:
        line = pipe.readline()
        queue.put(line, block=True)
        if line=="":
            break
</code></pre>

<p>This works well enough. In my UI I periodically do non-blocking "get"s from the queue. However, my problems come when I want to terminate the subprocess. (The subprocess is an arbitrary process, not something I wrote myself.) I can use the terminate method to terminate the process, but I do not know how to guarantee that my I/O thread will terminate. It will normally be doing blocking I/O on the pipe. This may or may not end some time after I terminate the process. (If the subprocess has spawned <em>another</em> subprocess, I can kill the first subprocess, but the second one will still keep the pipe open. I'm not even sure how to get such grand-children to terminate cleanly.) After that the I/O thread will try to enqueue the output, but I don't want to commit to reading from the queue indefinitely.</p>

<p>Ideally I would like some way to request termination of the subprocess, block for a short (&lt;0.5s) amount of time and after that be guaranteed that the I/O thread has exited (or will exit in a timely fashion without interfering with anything else) and that I can stop reading from the queue.</p>

<p>It's not critical to me that a solution uses an I/O thread. If there's another way to do this that works on Windows and Linux with Python 2.6 and a Tkinter GUI that would be fine.</p>

<p><hr></p>

<p>EDIT - Will's answer and other things I've seen on the web about doing this in other languages suggest that the operating system expects you just to close the file handle on the main thread and then the I/O thread should come out of its blocking read. However, as I described in the comment, that doesn't seem to work for me. If I do this on the main thread:</p>

<pre><code>process.stdout.close()
</code></pre>

<p>I get:</p>

<pre><code>IOError: close() called during concurrent operation on the same file object.
</code></pre>

<p>...on the main thread. If I do this on the main thread:</p>

<pre><code>os.close(process.stdout.fileno())
</code></pre>

<p>I get:</p>

<pre><code>close failed in file object destructor: IOError: [Errno 9] Bad file descriptor
</code></pre>

<p>...later on in the main thread when it tries to close the file handle itself.</p>
