我喜欢做一些东西,就像 shellscripting 中的 xargs 所做的那样。因此:
- 通过
exec()
系统调用运行外部命令 - 作为子进程
- 并行
- 等待执行(最好有超时)
如何在 perl 中完成?
最佳答案
您可以使用 Proc::Background
模块。
特别有趣的是子 timeout_system(..)
。
这是来自 Proc::Background 的示例模块页面:
use Proc::Background;
timeout_system($seconds, $command, $arg1);
timeout_system($seconds, "$command $arg1");
my $proc1 = Proc::Background->new($command, $arg1, $arg2);
my $proc2 = Proc::Background->new("$command $arg1 1>&2");
$proc1->alive;
$proc1->die;
$proc1->wait;
my $time1 = $proc1->start_time;
my $time2 = $proc1->end_time;
# Add an option to kill the process with die when the variable is
# DETROYed.
my $opts = {'die_upon_destroy' => 1};
my $proc3 = Proc::Background->new($opts, $command, $arg1, $arg2);
$proc3 = undef;
关于perl - perl 中的 xargs 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363301/