1

When closing my application, all threads, and Tkinter Threads close successfully but a subprocess that I have refuses to close on exit.

class ThreadedTask(Thread):

    def __init__(self, queue):
        Thread.__init__(self)
        self.queue = queue

    def run(self):
        proc = Popen("receivetest -f=/dev/pcan33".split(), stdout = PIPE)
        payload = iter(proc.stdout.readline, "")
        for line in payload:
            if line[0].isdigit():
                splitline = line.split()
                self.dictAdd(splitline)

This is the Class containing the subprocess.

And this is the call at the beginning:

if __name__ == "__main__":

    root = tk.Tk()

    Data = Queue.Queue()
    DataThread = ThreadedTask(Data)
    DataThread.daemon = True
    DataThread.start()

    myapp = BaudWindow(root)
    root.mainloop()

As I say everything else closes correctly. Is this due to the fact I have nested a subprocess into a thread?

Jim
  • 795
  • 4
  • 13
  • 28

2 Answers2

1

Child processes do not die automatically if the parent process dies by default. See Python: how to kill child process(es) when parent dies?

You could call proc.terminate() explicitly in your case e.g., in atexit handler.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

That is because you are not starting a subprocess but a separate thread. Try subprocess.popen() instead. It works as per what you want.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • But in my class definition I use popen: `def run(self): proc = Popen("receivetest -f=/dev/pcan33".split(), stdout = PIPE)` – Jim Mar 17 '15 at 09:14
  • But the parent thread to this subprocess is not your application thread. Your application thread gets terminated, but this subprocess would still be running because it's parent thread will still be running. In short, your application thread is not the parent to this subprocess. That's why it is not getting terminated. – Moinuddin Quadri Mar 17 '15 at 09:18