Python 如何在子流程运行时打印此代码的进度?
上面的代码仅在代码完成执行时显示rsync进度。我想打印正在发生的进展。例如,当文件传输时,我希望始终显示进度。我该怎么做Python 如何在子流程运行时打印此代码的进度?,python,rsync,Python,Rsync,上面的代码仅在代码完成执行时显示rsync进度。我想打印正在发生的进展。例如,当文件传输时,我希望始终显示进度。我该怎么做 import re import subprocess def sort(rprogress): '''This function extracts the percentage from the rsync progress and return strings of percentanges''' progre
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 = 'sachet.adhikari@69.43.202.97:/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)
stdout=subprocess.PIPE
阻止子流程打印到stdout,而是使用类似于bash中的方式将其传递到结果
sshpass -[args] rsync [source] [dest]
将打印进度,但
sshpass -[args] rsync [source] [dest] | sort
在流程完成之前不会打印任何内容
您需要的是tee
stdout
。看。根据这些答案,您可以做如下操作:
# 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)
stdout=subprocess.PIPE
阻止子流程打印到stdout,而是使用类似于bash中的方式将其传递到结果
sshpass -[args] rsync [source] [dest]
将打印进度,但
sshpass -[args] rsync [source] [dest] | sort
在流程完成之前不会打印任何内容
您需要的是tee
stdout
。看。根据这些答案,您可以做如下操作:
# 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)