如何从 Perl 中的管道进行非阻塞读取?
我有一个程序正在调用另一个程序并处理子程序的输出,即:
my $pid = open($handle, "$commandPath $options |");
现在我尝试了几种不同的方法来从句柄读取而不阻塞,但几乎没有成功。
我发现相关问题:
- perl-win32-how-to-do-a-non-blocking-read-of-a-filehandle-from-另一个进程
- why-does-my-perl-sysread-block-when-reading-from-a-socket
但他们遇到了以下问题:
ioctl
始终- 在 0 字节上使 perl
sysread
块崩溃(常见情况)
我不知道如何解决这个问题。
I have a program which is calling another program and processing the child's output, ie:
my $pid = open($handle, "$commandPath $options |");
Now I've tried a couple different ways to read from the handle without blocking with little or no success.
I found related questions:
- perl-win32-how-to-do-a-non-blocking-read-of-a-filehandle-from-another-process
- why-does-my-perl-sysread-block-when-reading-from-a-socket
But they suffer from the problems:
ioctl
consistently crashes perlsysread
blocks on 0 bytes (a common occurrence)
I'm not sure how to go about solving this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
管道在 Windows 上的功能不如在 Unix-y 系统上的功能。您不能对它们使用 4 参数
select
,并且默认容量很小。您最好尝试基于套接字或文件的解决方法。
现在您还有更多的蠕虫需要处理 - 定期运行
waitpid
来检查后台进程何时停止创建输出,调用seek $handle,0,1
从$handle
读取后清除 eof 条件,清理临时文件,但它有效。我已将
Forks::Super
模块写入处理这样的问题(以及许多其他问题)。对于这个问题,你可以像这样使用它Pipes are not as functional on Windows as they are on Unix-y systems. You can't use the 4-argument
select
on them and the default capacity is miniscule.You are better off trying a socket or file based workaround.
Now you have a couple more cans of worms to deal with -- running
waitpid
periodically to check when the background process has stopped creating output, callingseek $handle,0,1
to clear the eof condition after you read from$handle
, cleaning up the temporary file, but it works.I have written the
Forks::Super
module to deal with issues like this (and many others). For this problem you would use it like