2013-02-27 3 views
0

위의 코드는 코드 실행이 완료된 경우에만 rsync 진행률을 보여줍니다. 나는 그것이 진행되는대로 진전을 인쇄하고 싶다. 예를 들어 파일 전송 중일 때 진행 상황을 모두 보여주고 싶습니다. 어떻게해야합니까?하위 프로세스가 실행 중일 때이 코드의 진행률을 인쇄하는 방법은 무엇입니까?

import re 
    import subprocess 

    def sort(rprogress): 
     '''This function extracts the percentage from the rsync progress and return strings of percentanges''' 
     progress1 = re.findall(r'\d+%', rprogress) 
     remove_duplicate = set(progress1)  #Remove Duplicate percentage from list 
     remove_percent = [a.replace('%', '') for a in remove_duplicate]  #Removes percentage 
     sorted_list = sorted(remove_percent, key=int)  #Sort in ascending order 
     #result = ', '.join(map(lambda sorted_list: str(sorted_list) + '%', sorted_list)) #Adds the percentage 
     return sorted_list 


    source12 = '[email protected]:/home/sachet/my_files/ok.txt' 
    password = 'password' 
    destination = '/home/zurelsoft/files' 
    result = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination], 
             stdout=subprocess.PIPE).communicate()[0] 

    print result    


print sort(result) 

답변

0

stdout=subprocess.PIPE는 표준 출력에 인쇄에서 서브 프로세스를 방지하고 대신 bash는

sshpass -[args] rsync [source] [dest] 

진행을 인쇄하는 방법과 유사 communicate하지만

sshpass -[args] rsync [source] [dest] | sort 

하지 않습니다 인쇄를 사용하여 result에 전달 과정이 완료 될 때까지 아무 것도.

teestdout입니다. look here. 이러한 답변을 바탕으로 다음과 같이 할 수 있습니다.

# Caution! untested code 
result = [] 
process = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', 
          '--info=progress2', source12, destination], 
          stdout=subprocess.PIPE) 
while process.poll() is None: 
    line = process.stdout.readline() 
    print line 
    result.append(line) 
print sort(result) 
관련 문제