Python Subprocess.Popen from a thread

后端 未结 2 1894
孤街浪徒
孤街浪徒 2020-11-28 04:02

I\'m trying to launch an \'rsync\' using subprocess module and Popen inside of a thread. After I call the rsync I need to read the output as well. I\'m using the communicate

相关标签:
2条回答
  • 2020-11-28 04:19

    Here's a great implementation not using threads: constantly-print-subprocess-output-while-process-is-running

    import subprocess
    
    def execute(command):
        process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = ''
    
        # Poll process for new output until finished
        for line in iter(process.stdout.readline, ""):
            print line,
            output += line
    
    
        process.wait()
        exitCode = process.returncode
    
        if (exitCode == 0):
            return output
        else:
            raise Exception(command, exitCode, output)
    
    execute(['ping', 'localhost'])
    
    0 讨论(0)
  • 2020-11-28 04:24

    You didn't supply any code for us to look at, but here's a sample that does something similar to what you describe:

    import threading
    import subprocess
    
    class MyClass(threading.Thread):
        def __init__(self):
            self.stdout = None
            self.stderr = None
            threading.Thread.__init__(self)
    
        def run(self):
            p = subprocess.Popen('rsync -av /etc/passwd /tmp'.split(),
                                 shell=False,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
    
            self.stdout, self.stderr = p.communicate()
    
    myclass = MyClass()
    myclass.start()
    myclass.join()
    print myclass.stdout
    
    0 讨论(0)
提交回复
热议问题