Skip to content Skip to sidebar Skip to footer

Showing Progress While Spawning And Running Subprocess

I need to show some progress bar or something while spawning and running subprocess. How can I do that with python? import subprocess cmd = ['python','wait.py'] p = subprocess.Po

Solution 1:

Since the subprocess call is blocking, one way to print something out while waiting would be to use multithreading. Here's an example using threading._Timer:

import threading
import subprocess

classRepeatingTimer(threading._Timer):
    defrun(self):
        whileTrue:
            self.finished.wait(self.interval)
            if self.finished.is_set():
                returnelse:
                self.function(*self.args, **self.kwargs)


defstatus():
    print"I'm alive"
timer = RepeatingTimer(1.0, status)
timer.daemon = True# Allows program to exit if only the thread is alive
timer.start()

proc = subprocess.Popen([ '/bin/sleep', "5" ])
proc.wait()

timer.cancel()

On an unrelated note, calling stdout.read() while using multiple pipes can lead to deadlock. The subprocess.communicate() function should be used instead.

Solution 2:

As far as I see it all you need to do is put those reads in a loop with a delay and a print - does it have to be precisely a second or around about a second?

Post a Comment for "Showing Progress While Spawning And Running Subprocess"