【发布时间】:2014-01-07 03:18:00
【问题描述】:
我正在使用 TProcess 和来自thisfreepascal wiki 页面的建议读取 Lazarus 中的大型进程输出数据。
wiki 页面建议创建一个循环来读取流程输出数据,如下所示:
// ... If you want to read output from an external process, this is the code you should adapt for production use.
while True do
begin
MemStream.SetSize(BytesRead + 2024); // make sure we have room
NumBytes := OurProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);
if NumBytes > 0
then begin
Inc(BytesRead, NumBytes);
Write('.') //Output progress to screen.
end else
BREAK // Program has finished execution.
end;
// "Then read the MemStream to do your job"
维基页面还提到调用程序应该从输出管道中读取以防止它被填满。
那么,有多少数据会使输出管道变满?
为什么我们应该在上述循环中使用MemStream (TMemoryStream) 而不是直接从OurProcess.Output 流中读取(使用bytesAvailable 等)?
我正在从一个进程中读取 80MB 的 wav 数据,我注意到 MemStream 和 OurProcess.Output 流具有相同数量的数据!内存使用量翻倍。因此,wiki 建议的方法不能被认为是有效的或优化的。还是我遗漏了什么?
【问题讨论】:
-
现在有返回字符串的 runcommand() 宏。
标签: process stream freepascal lazarus