python - 连续获取输出: rsync info=progress2 call in python script

标签 python linux subprocess rsync popen

我正在使用 popen 调用 rsync,但输出并没有像在普通 Linux 中那样在我的 Web 应用程序的 python 脚本中连续打印出来。我正在尝试将一个目录上的所有文件复制到另一个目录(大量副本)。我想使用从输出更改收到的进度号来最终创建/更新我的网络应用程序中的进度栏​​。我想要的只是整个副本的总进度,因此我会在 rsync 命令中使用 --info=progress2 。 我也尝试过:

while True:
        line = self.proc.stdout.readline()
        if line != '':
            # the real code does filtering here
            print("test:", line.rstrip())
        else:
            break

但是等到最后才打印测试:b'' 我认为问题要么在于 while 循环提取数据,要么在于我如何使用不同的类将其打印到控制台。

使用这个--info=progress2的信息不多 因为这是一个相对较新的更新。

这是我的代码。

import subprocess
import logging
import sys
import os
import replicator.dfp.rep_utils as ru


class SyncProcessor(object):
    def __init__(self, src, dest):
        self.src = src
        self.dest = dest
        self.proc = None
        self.callback = None
        log_file = "sync-{}-{}.log".format(self.src, self.dest)
        self.sync_logger = ru.setup_logger(__file__, log_file, level=logging.DEBUG)

    def add_sync_callback(self, cb):
        self.callback = cb

    def run(self):
        print("Syncing Drive "+ str(self.src.driveNum) + " to Drive " + str(self.dest.driveNum))
        rsync_cmd = "sudo rsync -ah --info=progress2 --delete --stats /media/drive{}/ /media/drive{}".format(self.src.driveNum, self.dest.driveNum)
        self.proc = subprocess.Popen(rsync_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        while self.proc.poll() is None:
            output = self.proc.stdout.readline()
            if output == '':
                break
            if output:
                print("OUTPUT DECODE: " + output.decode("utf-8")
                #self.sync_logger.info(output.decode())
                self.callback.update(output.decode())
        print("<< Finished Syncing >>")
        #self.sync_logger.debug("<< Finished Syncing >>")
        rc = self.proc.poll()
        #self.sync_logger.debug("Return code: {}".format(rc))
        os.system("sync")
        return rc

    def communicate(self):
        return self.proc.communicate()

class Progress(object):
    """Callback to report progress of a SyncProcessor"""
    def __init__(self, src, dest, out=sys.stdout):
        self.src = src
        self.dest = dest
        self.out = out

    def update(self, data):
        line = "From Progress({}-{}) -> {}"
    self.out.write(line.format(self.src, self.dest, data))

最佳答案

所以我意识到百分比从 0-100% 的整个变化被视为一行,因为它是由\r 而不是\n 分隔的

self.proc.stdout.readline()

因此该行仅在进程达到 100% 后激活

我将其切换为 self.proc.stdout.readline(80) 它每 80 个字符打印一次,为我提供有关百分比的更新。然而,由于行的长度在整个过程中都在变化,我正在寻找更好的方法来做到这一点

关于python - 连续获取输出: rsync info=progress2 call in python script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55052122/

相关文章:

python子进程模块无法执行shell

python - 在 Mac OS X 上捕获 Python 子进程输出时出现问题

python - 删除实例时删除属性

linux - 如何在 Linux 中将一些文本 append 到文件夹中的所有文件

python - 在由字符串格式的值组成的字典python中查找具有最小值的键

linux - 向 Yocto 生成的自定义 Linux 镜像添加新内核参数

linux - bash 脚本语言使用循环打印数字列表

python - 什么时候 'commands' 比 'popen' 子进程更可取?

python - 监控远程FTP目录

python - 将大型数组变量(类型 = 对象)导出到 CSV 文件