PHP 的 proc_open()、proc_close() 等的 Perl 等价物是什么?

发布于 2024-09-26 01:14:46 字数 670 浏览 3 评论 0原文

使用 PHP 的 proc_open(),我可以启动一个进程,在进程运行时使用 fread()STDOUTSTDERR(分别)一次读取任意数量的字节,在 STDOUTSTDERR 管道上使用 feof() 检测进程何时完成,然后使用 proc_close() 获取进程的退出代码。我已经用 PHP 完成了所有这些工作。它运作良好,给了我很多的控制权。

有没有办法在 Perl 中完成所有这些事情?总而言之,我需要能够执行以下操作:

  • 启动外部进程
  • 读取 STDOUTSTDERR 分别
  • 读取 STDOUTSTDERR 进程运行时一次任意数量的字节(即无需等待进程完成)
  • 检测进程何时完成
  • 获取进程的退出代码

提前感谢您的回答。

Using PHP's proc_open(), I can start a process, read from STDOUT and STDERR (separately) an arbitrary number of bytes at a time using fread() while the process is running, detect when the process is done using feof() on the STDOUT and STDERR pipes, and then use proc_close() to get the exit code of the process. I've done all of this in PHP. It works well, and gives me a lot of control.

Is there a way to do all of these things in Perl? To summarize, I need to be able to do the following:

  • start an external process
  • read STDOUT and STDERR separately
  • read STDOUT and STDERR an arbitrary number of bytes at a time while the process is running (i.e. without having to wait for the process to finish)
  • detect when the process is finished
  • get the exit code of the process

Thanks in advance for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笔绘流年 2024-10-03 01:14:46

您可以使用 Perl 的系统调用接口推出您自己的解决方案,但使用内置模块 IPC::Open3 更容易。至于您的列表:

启动外部进程:

use IPC::Open3;
use IO::Handle;
use strict;

my $stdout = IO::Handle->new;
my $stderr = IO::Handle->new;
my $pid = open3(undef, $stdout, $stderr, 'my-command', 'arg1', 'arg2');

分别读取 STDOUT 和 STDERR,一次任意数量的字节:

my $line = <$stdout>;
# Or
sysread $stderr, my $buffer, 1024;

检测进程何时完成:

use POSIX qw(sys_wait_h);

waitpid $pid, 0;  # Waits for process to terminate
waitpid $pid, WNOHANG;  # Checks if the process has terminated

获取进程的退出代码:

my $status = $?;  # After waitpid indicates the process has exited

请务必阅读 IPC::Open3 文档;正如它警告的那样,如果您不小心,当您有单独的 stdout 和 stderr 管道时,很容易陷入僵局。如果子进程填充任一管道,它将阻塞,如果父进程读取其他管道,将阻塞。

You could roll your own solution using Perl's system call interface, but it's easier to use the built-in module IPC::Open3. As for your list:

Start an external process:

use IPC::Open3;
use IO::Handle;
use strict;

my $stdout = IO::Handle->new;
my $stderr = IO::Handle->new;
my $pid = open3(undef, $stdout, $stderr, 'my-command', 'arg1', 'arg2');

Read STDOUT and STDERR separately, an arbitrary number of bytes at a time:

my $line = <$stdout>;
# Or
sysread $stderr, my $buffer, 1024;

Detect when the process is finished:

use POSIX qw(sys_wait_h);

waitpid $pid, 0;  # Waits for process to terminate
waitpid $pid, WNOHANG;  # Checks if the process has terminated

Get the exit code of the process:

my $status = $?;  # After waitpid indicates the process has exited

Be sure to read the IPC::Open3 documentation; as it warns, it's easy to get yourself deadlocked when you have separate stdout and stderr pipes, if you're not careful. If the child process fills either pipe, it will block, and if the parent process reads the other pipe, it will block.

悲欢浪云 2024-10-03 01:14:46

您需要此模块: IPC::Open3

You want this module: IPC::Open3

凉宸 2024-10-03 01:14:46

你想要IPC::Run,它捕获IO并返回退出值

You want IPC::Run, it captures the IO and returns the exit value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文