Ethan
Ethan

Reputation: 261

Paramiko & rsync - Get Progress asynchronously while command runs

I am using paramiko to run rsync on a remote machine. When i use stdout.readlines() it blocks my program and outputs a ton of lines after the command ends. I know rsync progress always updates its output. How do i read the output every interval without waiting for the command to finish (i am transferring a very large file).

import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(...)

stdin, stdout, stderr = ssh.exec_command("rsync...")
counter = 0
while True:
    counter += 1
    print stdout.readlines(), stderr.readlines(), counter
    time.sleep(3)

Upvotes: 2

Views: 2284

Answers (1)

sethmlarson
sethmlarson

Reputation: 941

Instead of using readlines() you should use read([bytes]) to progressively read output. readlines() reads all lines until EOF (that's why you see the blocking) and then splits into lines on the \n character.

Instead, do something like this:

while True:
    counter += 1
    print(stdout.read(2048), stderr.read(2048), counter)
    time.sleep(3)

Note: This doesn't ever terminate the loop, you might want to consider terminating the loop if the output from both stdout and stderr have length zero.

Upvotes: 1

Related Questions