2012-12-24 6 views
6

Ich versuche, eine Funktion zu schreiben, die die Variable alle 1 Minute liest und den Wert jedes Mal zurückgibt. Der Variablenname lautet proc:Wie schreibe ich eine Polling-Funktion in Python?

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

Der Fortschritt wird in proc-Variable gespeichert. Ich möchte, dass die Funktion die Variable alle 1 Minute abfragt und den Wert zurückgibt. Dies wird durchgeführt, bis die Variable ausgeführt wird. Was ist der beste Weg, es zu tun?

mit Versuchte:

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) 

obwohl kein Glück! Es druckt den gesamten Fortschritt zur gleichen Zeit und Schleife über.

+0

Sie können dies überprüfen, http://stackoverflow.com/questions/1038907/run-a-task-at-specific- Intervalle-in-Python –

Antwort

4

Der folgende Code wird rsync ausführen und jede Ausgabe des Befehls lesen, sobald sie verfügbar ist. Ich habe die Option --progress von rsync verwendet, um den Fortschritt auszudrucken. Die Fortschrittsaktualisierungen enden manchmal in einem \n und manchmal in einem \r. Also lese ich einzelne Zeichen und formiere dann jede Zeile von Zeichen, indem ich nach einem dieser beiden Zeichen suche. Immer wenn ich auf eine neue Fortschrittslinie stoße, drucke ich sie auf den Bildschirm. Sie können jedoch auch etwas anderes tun, wie zum Beispiel das Analysieren des Prozentabschlusses und das Anzeigen eines grafischen Fortschrittsbalkens. Wenn Sie daran interessiert sind, einen Fortschrittsbalken in Terminal check this answer out zu erstellen. Ich gebe einen Beispielaufruf der Sync-Funktion und Beispielausgabe.

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') 

Ausgang

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 
+0

Vielen Dank für Ihre Antwort. Wie zeige ich das gleiche in Django (im Webbrowser?) – user1881957

+0

@ user1881957 Blick auf http://StackOverflow.com/q/2922874/1699750. Schau dir meine Antwort und die anderen Antworten an. –

+0

Das war ziemlich hilfreich, aber nicht zu spezifisch für meine Frage. Können Sie bitte Ihre Antwort bearbeiten? – user1881957