File: pymailgui-products/unzipped/PyMailGui-PP4E/PP4E/Gui/Tools/pipe_gui3.py
""" read command pipe in a thread and place output on a queue checked in timer loop; allows script to display program's output without being blocked between its outputs; spawned programs need not connect or flush, but this approaches complexity of sockets """ import _thread as thread, queue, os from tkinter import Tk from PP4E.Gui.Tools.guiStreams import GuiOutput stdoutQueue = queue.Queue() # infinite size def producer(input): while True: line = input.readline() # OK to block: child thread stdoutQueue.put(line) # empty at end-of-file if not line: break def consumer(output, root, term='<end>'): try: line = stdoutQueue.get(block=False) # main thread: check queue except queue.Empty: # 4 times/sec, OK if empty pass else: if not line: # stop loop at end-of-file output.write(term) # else display next line return output.write(line) root.after(250, lambda: consumer(output, root, term)) def redirectedGuiShellCmd(command, root): input = os.popen(command, 'r') # start non-GUI program output = GuiOutput(root) thread.start_new_thread(producer, (input,)) # start reader thread consumer(output, root) if __name__ == '__main__': win = Tk() redirectedGuiShellCmd('python -u pipe-nongui.py', win) win.mainloop()