3

I'm working on a multi-thread python script where I have a dedicated thread in charge of executing some shell commands without re-opening a brand new zsh shell but keeping the same session alive.

The main thread puts the to-be-executed commands in a queue which is shared with the thread in charge of executing the commands.

import threading, Queue
class ShellThread(threading.Thread):
    def __init__(self, command_q, command_e):
        super(ShellThread, self).__init__()
        self.command_q = command_q
        self.command_e = command_e
        self.stoprequest = threading.Event()

        from subprocess import Popen, PIPE
        import os
        self.zsh = Popen("zsh", stdin=PIPE, stdout=PIPE)

    def run(self):
        while not self.stoprequest.isSet():
            try:
                command = self.command_q.get(True, 0.1)
                print "ShellThread is now executing command : " + command
                self.zsh.stdin.write(command + '\n')
                self.zsh.stdin.flush()
                self.command_e.set()

            except Queue.Empty:
                continue

    def join(self, timeout=None):
        self.stoprequest.set()
        self.zsh.stdin.close()
        super(ShellThread, self).join(timeout)

def main(args):
    __command_q = Queue.Queue()
    __command_e = threading.Event()
    __thread = ShellThread(command_q=__command_q, command_e=__command_e)
    __thread.start()

    while 1:
        line = raw_input()
        print 'MainThread : ' + line
        __command_q.put(line)
        __command_e.wait(0.5)
        __command_e.clear()

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

It does work but I have random IOError: [Errno 32] Broken pipe errors and I still haven't found a way to get the stdout after executing each command.

UPDATE: Please note that the whole point of this is to keep one and only zsh shell opened (this is why I have a dedicated thread for this purpose) to run distinct commands in time. I cannot use Popen.communicate since it would close the shell once the command is over and I do not know all the commands I will have to run up front.

codeJack
  • 2,423
  • 5
  • 24
  • 31

0 Answers0