Jonathan
Jonathan

Reputation: 11321

Using the python sh module, how can I show the output of a command as it's happening?

The python sh module seem to wait until a command or at least a line is finished before it can show any of the output. How can I show the output of a command as it's happening?

Here's what I've tried so far. This is for git cloning something using sh.

for line in sh.git.clone(url, '--progress', '--recursive', _err_to_out=True, _iter=True):
    print(line)

And this prints every line after it's completed, but doesn't print it out in real time. So when I'm git cloning something, it doesn't show the progress of the clone, since it waits for the line to finish before it returns it. How can I have the sh module print out a command's progress in real time?

Upvotes: 1

Views: 946

Answers (1)

Jose Raul Barreras
Jose Raul Barreras

Reputation: 859

Use the _out_bufsize parameter. In my case, works OK with 100...

for line in sh.git.clone(url, '--progress', '--recursive', _err_to_out=True, _iter=True, _out_bufsize=100):
    print(line)

Output:

Cloning into 'some_project'...
remote: Counting objects: 70796, done.
remote: Compressing objects: 0
remote: Compressing objects: 1% (203/20259)
remote: Compressing objects: 3% (608/20259)
remote: Compressing objects: 5% (1013/20259)
remote: Compressing objects: 7% (1419/20259)
remote: Compressing objects: 9% (1824/20259)

Upvotes: 4

Related Questions