Joy*_*ind 5 python rsync progress-bar
我正在使用rsyncwith--info-progress选项的开发版本。我正在编写一个 python 程序,它使用 rsync 将文件从服务器传输到本地计算机:
finalresult = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', 'hostname:/filename', '/home/nfs/django/user'],
stdout=subprocess.PIPE).communicate()[0]
Run Code Online (Sandbox Code Playgroud)
当我在终端中运行程序时,它不会向我显示进度,但是如果我独立使用 rsync 实用程序,它会显示进度。我想在终端中查看进度并解析或获取进度百分比以供稍后使用以实时向用户显示进度条。谢谢!
编辑:
这是我到目前为止所尝试的:上面的代码将命令处理存储在finalresult变量中。我试图finalresult在 django 模板中打印变量,但它没有返回任何内容。
抱歉,但这可能无法通过subprocess标准库的其余部分来完成。那是因为在里面subprocess.communicate()你会发现:
def wait(self):
"""Wait for child process to terminate. Returns returncode
attribute."""
if self.returncode is None:
# Try calling a function os.waitpid(self.pid, 0)
# Ignore Interrupted System Call (errno.EINTR) errors
pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0)
self._handle_exitstatus(sts)
return self.returncode
Run Code Online (Sandbox Code Playgroud)
在 Unix 上,os.waitpid() 函数调用意味着:等待由进程 id pid 给出的子进程完成,并返回包含其进程 id 和退出状态指示的元组。因此,您必须等到子进程完成。
最接近的替代方案是pexpect,但它仍然不会进行进度条分析。一个可能的解决方案是破解 rsync 并修补其进度输出,以便 pexpect 可以使用它。
| 归档时间: |
|
| 查看次数: |
1963 次 |
| 最近记录: |