python - 如何在 Python 中显示和解析 rsync 进度?

标签 python progress-bar rsync

我正在使用带有 --info-progress 选项的 rsync 的开发版本。我正在编写一个 python 程序,它使用 rsync 将文件从服务器传输到本地计算机:

finalresult = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', 'hostname:/filename', '/home/nfs/django/user'],
                                    stdout=subprocess.PIPE).communicate()[0]

当我在终端中运行程序时,它不会显示进度,但是如果我独立使用 rsync 实用程序,它会显示进度。我想在终端中查看进度并解析或获取进度百分比以便稍后使用它向用户实时显示进度条。谢谢你!

编辑: 到目前为止,这是我尝试过的: 上面的代码将命令处理存储在 finalresult 变量中。我试图在 django 模板中打印 finalresult 变量,但它没有返回任何内容。

最佳答案

抱歉,但这可能无法通过 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

在 Unix 上,os.waitpid() 函数调用意味着:WAITING由进程 ID pid 给出的子进程完成,并返回一个包含其进程 ID 和退出状态指示的元组。所以,你必须等到子进程完成。

最接近的选择是 pexpect ,但它仍然不会进行进度条分析。一个可能的解决方案是破解 rsync 并修补其进度输出,以便 pexpect 可以使用它。

关于python - 如何在 Python 中显示和解析 rsync 进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13578281/

相关文章:

python - 如何将 C++ for 循环转换为 Python?

python - ParDo 中的分区和多个输出之间的区别?

python - 查找集合的所有不相交子集,尊重元素顺序

android - 进度条设置可见不起作用

linux - Rsync 与 .dd.xz 文件

python - 获取错误 : KeyError: 'Only the Series name can be used for the key in Series dtype mappings.' when trying to do pandas Smote algorithm

javascript - 如何更新进度条?

C# WebClient 使用异步并返回数据

mysql - galera 节点上的完整 SST 未启动 ("WSREP: Prepared SST request"缺失)

docker - 如何通过 ssh 将 "rsync"docker 镜像发送到远程主机?