How to write a polling function in Python?

user1881957 picture user1881957 · Dec 24, 2012 · Viewed 11.7k times · Source

I am trying to write a function that reads the variable every 1 minute and return the value of each time. The variable name is proc:

proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], 
                                    stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]

The progress is stored in proc variable. I want the function to poll the variable every 1 minute and return value. This is done until the variable is upon execution. What's the best way to do it?

Tried using:

def doWork():
    while True:
        proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], 
                                    stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]stdout=subprocess.PIPE).communicate()[0]
        data = sort(proc)
        print data 
        time.sleep(10)

No luck though! It prints the entire progress at same time and loop over.

Answer

Marwan Alsabbagh picture Marwan Alsabbagh · Dec 24, 2012

The code below will run rsync and read any output from the command the moment it is available. I used the --progress option of rsync to have it print out it's progress. The progress updates sometimes end in a \n and sometimes in a \r. So I read individual characters and then form each line of characters by looking for either of those two characters. Whenever I encounter a new progress line I print it to the screen. You could chose to do anything though such as parsing the percent complete and displaying a graphical progress bar. If you are interested in seeing how to produce a progress bar in terminal check this answer out. I put an example call of the sync function and example output.

code

from subprocess import Popen, PIPE

def sync(src, dest, passwd):
    cmd = ['sshpass', '-p', passwd, 'rsync', '-avz', '--progress', src, dest]
    p = Popen(cmd, stdout=PIPE)
    line = ''
    while True:
        c = p.stdout.read(1)
        if not c:
            break
        if c in ['\n', '\r']:
            print 'rsync progress: %s' % line
            line = ''
        else:
            line += c

sync('/path/big.txt', 'myserver:/path/', 'mypassword')

output

rsync progress: sending incremental file list
rsync progress: big.txt
rsync progress: 
rsync progress:        32768   0%    0.00kB/s    0:00:00  
rsync progress:     65798144  31%   62.72MB/s    0:00:02  
rsync progress:    131596288  62%   62.77MB/s    0:00:01  
rsync progress:    197427200  94%   62.79MB/s    0:00:00  
rsync progress:    209715200 100%   62.80MB/s    0:00:03 (xfer#1, to-check=0/1)
rsync progress: 
rsync progress: sent 204032 bytes  received 31 bytes  45347.33 bytes/sec
rsync progress: total size is 209715200  speedup is 1027.70