我想这真的很简单,但我做不出来。在
我正在尝试将DD
映像子进程的输出实时写入日志文件—我使用DD v 8.25
,从中可以使用写入stderr
的'status=progress'
选项定期更新进度。在
我可以通过将file对象传递给stderr
来让它实时记录完整的输出
log_file = open('mylog.log', 'a')
p = subprocess.Popen['dd command...'], stdout=None, stderr=log_file)
…但我更愿意先截取stderr
中的字符串,以便在写入文件之前对其进行解析。在
我尝试过线程化,但似乎无法让它写入,或者,如果它写了,它只在进程结束时执行,而不是在进程期间。在
我是一个python noob,所以示例代码将不胜感激。谢谢!在
更新-正在工作(ISH)
我看了一下J.F.Sebastian建议的链接,找到了关于使用线程的帖子,所以在那之后我使用了“kill-USR1”技巧让DD将进度发布到stderr,然后我就可以拿起它:
^{pr2}$唯一的问题是我不知道如何提高绩效。我只需要它每隔2秒左右报告一次状态,但是增加时间延迟会增加成像的时间,这是我不想要的。这是另一篇文章的问题。。。在
感谢塞巴斯蒂安和阿里。在
在这个例子中,可以(使用python 3)从stderr流到控制台:
流式传输到文件:
^{pr2}$要在终端中显示
dd
的进度报告并将输出保存(解析)到日志文件中:注
shell=True
:这里不需要shell。Popen()
可以直接运行dd
while proc1.poll() == None
您不需要它(如果proc1.poll()
不是None,您将在proc1.stderr
上看到EOF)。您可能会丢失数据(即使进程已经退出,也可能存在缓冲内容)。无关:如果需要与None
进行比较,请使用is None
而不是{io.TextIOWrapper(newline='')
启用文本模式 (它使用locale.getpreferredencoding(False)
)并且 也将'\r'
视为换行符bufsize=-1
(请参见^{相关问题 更多 >
编程相关推荐