Skip to content

Instantly share code, notes, and snippets.

@zed zed/
Last active May 27, 2018

What would you like to do?
#!/usr/bin/env python3
- read subprocess output without threads using a socket pair
- show the output in a tkinter GUI (while the process is still running)
- stop subprocess on a button press
import logging
import os
import socket
import sys
import tkinter as tk
from subprocess import Popen, STDOUT
logger = logging.getLogger(__name__)
# define dummy command to generate some output
cmd = [
sys.executable or "python",
import itertools, time
for i in itertools.count():
class ShowProcessOutputDemo:
def __init__(self, root):
"""Start subprocess, make GUI widgets."""
self.root = root
# start subprocess
self.read_sock, write_sock = socket.socketpair()
self.proc = Popen(cmd, stdout=write_sock, stderr=STDOUT)"started")
# show subprocess' stdout in GUI
self.root.createfilehandler(self.read_sock, tk.READABLE, self.read_output)
self._var = tk.StringVar() # put subprocess output here
tk.Label(root, textvariable=self._var).pack()
# stop subprocess using a button
tk.Button(root, text="Stop subprocess", command=self.stop).pack()
def read_output(self, pipe, mask, bufsize=8192):
"""Read subprocess' output, pass it to the GUI."""
data =, bufsize)
if not data: # clean up"eof")
self.root.after(5000, self.stop) # stop in 5 seconds
logger.debug("got: %r", data)
def stop(self, stopping=[]):
"""Stop subprocess and quit GUI.""""stopping")
self.proc.wait() # wait for the subprocess' exit
self.root.destroy() # exit GUI
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
root = tk.Tk()
app = ShowProcessOutputDemo(root)
root.protocol("WM_DELETE_WINDOW", app.stop) # exit subprocess if GUI is closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.