Skip to content

Instantly share code, notes, and snippets.

@zed zed/kill-process.py Secret
Last active Jul 1, 2019

Embed
What would you like to do?
#!/usr/bin/env python
"""
- read output from a subprocess in a background thread
- show the output in the GUI
- stop subprocess using a Tkinter button
https://stackoverflow.com/questions/15362372/display-realtime-output-of-a-subprocess-in-a-tkinter-widget
"""
from __future__ import print_function
import sys
from subprocess import Popen, PIPE, STDOUT
from textwrap import dedent
from threading import Thread
try:
import Tkinter as tk
except ImportError:
import tkinter as tk # Python 3
info = print
class StopProcessDemo:
def __init__(self, root):
self.root = root
# show subprocess' stdout in GUI
self._var = tk.StringVar() # put subprocess output here
tk.Label(root, textvariable=self._var).pack()
# to access event's .data use tcl to bind the event
# https://stackoverflow.com/questions/41912004/how-to-use-tcl-tk-bind-function-on-tkinters-widgets-in-python
on_reading_line = root.register(self._var.set)
root.tk.call("bind", root, '<<line>>', on_reading_line + " %d")
# stop subprocess using a button
tk.Button(root, text="Stop subprocess", command=self.stop).pack()
for t in range(1):
# start dummy subprocess to generate some output
self.process = Popen([sys.executable, "-u", "-c", dedent("""
import itertools, sys, time
for i in itertools.count():
print(i, sys.argv[1])
time.sleep(0.1)
"""), str(t)], stdout=PIPE, stderr=STDOUT, universal_newlines=True)
# launch thread to read the subprocess output:
# process.readline -> event <<line>> -> label
t = Thread(target=self.reader_thread,
# https://mail.python.org/pipermail/tkinter-discuss/2013-November/003526.html
args=[lambda line: root.event_generate('<<line>>', when='tail', data=line)])
t.daemon = True # close pipe if GUI process exits
t.start()
def reader_thread(self, emit):
"""Read subprocess output and emit lines."""
with self.process.stdout as pipe:
for line in iter(pipe.readline, ''):
emit(line)
def stop(self):
"""Stop subprocess and quit GUI."""
info('stoping')
self.process.terminate() # tell the subprocess to exit
# kill subprocess if it hasn't exited after a countdown
def kill_after(countdown):
if self.process.poll() is None: # subprocess hasn't exited yet
countdown -= 1
if countdown < 0: # do kill
info('killing')
self.process.kill() # more likely to kill on *nix
else:
self.root.after(100, kill_after, countdown)
return # continue countdown
# clean up
self.process.stdout.close() # close fd
self.process.wait() # wait for the subprocess' exit
self.root.destroy() # exit GUI
kill_after(countdown=5)
root = tk.Tk()
app = StopProcessDemo(root)
root.protocol("WM_DELETE_WINDOW", app.stop) # exit subprocess if GUI is closed
root.mainloop()
info('exited')
@Golflengte

This comment has been minimized.

Copy link

Golflengte commented Jan 3, 2015

Finally some good example.
To use it in windows 8 and Linux I made some modifications. Some buttons to start some subprocesses
Using dir in windows and ls in Linux, Get_iplayer to download some BBC programs.
Still working on improvements, but as being just a Python beginner, like to know if I can still improve my code.
Here my code :

!/usr/bin/python3.4.2

-- coding:utf-8 --

programname = 'tktq02WL.py' test Windows

date 02012015

https://gist.githubusercontent.com/zed/42324397516310c86288/raw/8e156ef9d84f5be258fe96696a24b7d2c5ba22b9/kill-process.py

"""

  • read output from a subprocess in a background thread
  • show the output in the GUI
  • stop subprocess using a Tkinter button
  • select subprocess with a button
  • for windows make a "dir.cmd " file (exec a dir of a directory)
    make a "get_iplayer" file (execution of get_iplayer)
    install these files in the Python directory
    """
    from future import print_function
    from collections import deque
    from itertools import islice
    from subprocess import Popen, PIPE, STDOUT
    from threading import Thread
    import subprocess
    import sys
    import time
    from time import ctime
    import itertools
    try:
    import Tkinter as tk
    except ImportError:
    import tkinter as tk # Python 3

try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # Python 3
try:
from tkinter import *
except:
from Tkinter import *
import tkinter as tk
programname = 'tkq02WL.py'
dirtext = ""
dirtextl = "ls -ial"
dirtextw = "dir"
dircmd = ""
recordir = ""
recordirw = 'D:/Video-lib/'
recordirl = "/home/bob/Video"

info = print
print ("Make choice for a process by pressing a button")

def iter_except(function, exception):
"""Works like builtin 2-argument iter(), but stops on exception."""
try:
while True:
yield function()
except exception:
return

class Application(tk.Frame):

def init(self, master=None):

Frame.init(self, master)

self.createWidgets()

class Application():
def init(self,root):
self.root = root
self.createWidgets()
#self.root.after(60,self.displaytime)
self.displaytime()
if sys.platform == "linux":
self.recordir =recordirl
self.dircmd = "ls -ial"
self.getiplayercmd = "get_iplayer"
else:
self.recordir = recordirw
self.dircmd = "dir.cmd"
self.getiplayercmd = "get_iplayer.cmd"
#print (self.dircmd)
#print (self.recordir)

Frame.init(self, master)

self.createWidgets()

def createWidgets(self):
    self.var = StringVar() # put subprocess output here
    self.dtm = StringVar()
    self.upper_frame = Frame(background='green', borderwidth=5)
    self.upper_frame.grid(row=0,column=0,columnspan=3,sticky=N+S+E+W)
    self.bt11 = Button( text = 'Stop subprocess',justify=RIGHT,  command = self.stop)
    self.bt11.grid(row =0,column=1,sticky=E)                      #locatio      
    if sys.platform == "linux":
        dirtext = dirtextl
    else:
        dirtext = dirtextw
    self.bt12 = Button( text = dirtext,justify=RIGHT,  command = self.dircmd)
    self.bt12.grid(row =0,column=2,sticky=E)                      #locatio
    self.bt13 = Button( text = 'index',justify=RIGHT,  command = self.index)
    self.bt13.grid(row =0,column=3,sticky=E)                      
    self.bt14 = Button( text = 'index_cook',justify=RIGHT,  command = self.index_cook)
    self.bt14.grid(row =0,column=4,sticky=E)
    self.bt15 = Button( text = 'download',justify=RIGHT,  command = self.downld)
    self.bt15.grid(row =0,column=5,sticky=E) 
    self.bt16 = Button( text = 'UK-download',justify=RIGHT,  command = self.UKdownld)
    self.bt16.grid(row =0,column=6,sticky=E) 

    self.btn_frame = Frame(background='grey', borderwidth=5)     # buttons
    self.btn_frame.grid(column=0,columnspan=3,sticky=N+S+E+W)
    self.lblfs12=Label(self.btn_frame,textvariable=self.var, justify=LEFT)
    self.lblfs12.grid(row=2,column=1,ipady=1,padx=1)
    #*****Date and Time
    self.lbldtm=Label(textvariable=self.dtm)
    self.lbldtm.grid(row=5,column=0,sticky=W)   # location of date and time
    #self.lbldtm.grid(row=5,column=0)  
def displaytime(self):
    tim=ctime()
    self.dtm.set(tim)
    self.root.after(40,self.displaytime)  

class StopProcessDemo:

def init(self, root):

self.root = root

    # start dummy subprocess to generate some output

self.proc = Popen(["python", "-u", "-c", """

import itertools, sys, time

for i in itertools.count():

print(i)

time.sleep(0.001)

"""], stdout=PIPE, stderr=STDOUT)

start a subproces by choosing a button...

def dircmd(self):       
    args = (self.dircmd+" "+self.recordir)       
   # args =( "D: "+"cd D:/Video-lib "+"dir/w")
    #args = self.dircmd
    self.call_subprocess(args)
def index(self):
    args = self.getiplayercmd
    self.call_subprocess(args)
def index_cook(self):
    args = self.getiplayercmd + " cook"
    self.call_subprocess(args)
def downld(self):
    #recordir = 'D:/Video-lib/'
    #hostx = "http://164.215.111.16"
    #hostx = "http:/176.35.95.236"
    #hostx = "http://212.71.237.49"
    hostx = "http://212.219.231.232"
    portx = "8080"
    args =  (self.getiplayercmd +
             " -g"+
             " 679" +
             " --mode=best" +
             " --subtitles"+
             " --nopurge" +
             " --attempts=99" +
             #"--verbose""--force" +
             " --output=" + self.recordir +
             "-p" +
             hostx + ":" + portx)
    self.call_subprocess(args)
def UKdownld(self):    
    args =  (self.getiplayercmd +
             " -g" +
             " 680")
    self.call_subprocess(args)

def call_subprocess(self,args):        
    print ("args=",args,end="\r\n",flush=True)
    # start  subprocess to generate some output
    # self.proc = subprocess.Popen(args,shell=True,
    #                               bufsize=1,
    #                               universal_newlines=True)
    self.proc = subprocess.Popen(args,shell=True,
                                 stdout=subprocess.PIPE,
                                 bufsize=1)


    # launch thread to read the subprocess output
    #   (put the subprocess output into the queue in a background thread,
    #    get output from the queue in the GUI thread.
    #    Output chain: proc.readline -> queue -> stringvar -> label)
    q = Queue()
    t = Thread(target=self.reader_thread, args=[q]).start()
    # show subprocess' stdout in GUI
    ##self._var = tk.StringVar() # put subprocess output here
    #tk.Label(root, textvariable=self._var).pack()
    self.update(q) # start update loop

    # stop subprocess using a button 

def reader_thread(self, q):
    linecount = 0
    """Read subprocess output and put it into the queue."""
    for line in iter(self.proc.stdout.readline, b''):
        linecount =  linecount + 1
        q.put(line)
    timecount = linecount / 200   
    time.sleep(timecount)    
    info('done reading')
    print ("linecount =",linecount,end="\r\n",flush=True)
    print ("timecount =",timecount,end="\r\n",flush=True)

def update(self, q):
    #global linecount
    """Update GUI with items from the queue."""
    # read no more than 10000 lines, use deque to discard lines except the last one, 

for line in deque(islice(iter_except(q.get_nowait, Empty), 10000), maxlen=1):

if line is None:

return # stop updating

else:

self._var.set(line) # update GUI

print added

    while not q.empty():
        line = q.get()
        if line is None:
            return # stop updating
        else:        
            self.var.set(line) # update GUI
           # print (line,end="\r\n",flush=True)
            oneline = line.rstrip()
            #linecount =  linecount + 1
            print (oneline.decode("latin"),end="\r\n",flush=True)

    self.root.after(40, self.update, q) # schedule next update      

def stop(self):
    """Stop subprocess and quit GUI."""
    info('stopping')
    self.proc.terminate() # tell the subprocess to exit

    # kill subprocess if it hasn't exited after a countdown
    def kill_after(countdown):
        if self.proc.poll() is None: # subprocess hasn't exited yet
            countdown -= 1
            if countdown < 0: # do kill
                info('killing')
                self.proc.kill() # more likely to kill on *nix                    
            else:
                self.root.after(1000, kill_after, countdown)
                return # continue countdown in a second
        # clean up
        self.proc.stdout.close()  # close fd
        self.proc.wait()          # wait for the subprocess' exit
        self.root.destroy()       # exit GUI
    kill_after(countdown=5)

def main():
root = Tk()
app=Application(root)
app.root.title(programname)
root.protocol("WM_DELETE_WINDOW", app.stop) # exit subprocess if GUI is closed
root.mainloop()
info('exited')
if name == 'main':
main()

@zed

This comment has been minimized.

Copy link
Owner Author

zed commented Jan 16, 2015

@Golflengte: the code is unreadable (formatting). You could post a link where the code is properly formatted instead.

@sandba66er

This comment has been minimized.

Copy link

sandba66er commented Oct 25, 2017

God bless you! I'm working on my first python project with a limited coding background. After weeks of researching the possible methods to stream live data to my graph while keeping it interactive, I think this is the winner.

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.