[英]Perl - parallel programming - running two external programs
我有一个Perl脚本,用于运行一系列数据集的两个外部程序,一个依赖另一个程序。 当前,我只是一次对每个数据集执行一次,通过第一个程序运行它,使用qx收集结果,然后使用这些结果运行第二个程序。 数据将与第二个程序的结果添加到输出文件中,每个数据集一个文件。 我创建了一个简单的可重现的示例,希望可以捕捉到我当前的方法:
#!/usr/bin/perl
#
# stackoverflow_q_7-7-2016.pl
use warnings;
use strict;
my @queries_list = (2, 4, 3, 1);
foreach my $query (@queries_list) {
#Command meant to simulate the first, shorter process, and return a list of results for the next process
my $cmd_1 = "sleep " . $query . "s; shuf -i 4-8 -n 3";
print "Running program_1 on query $query...\n";
my @results = qx($cmd_1);
foreach (@results) {
chomp $_;
#Command meant to simulate a longer process whose input depends on program_1; the output I write to a separate file for each query
my $cmd_2 = "sleep " . $_ . "s; fortune -s | head -c " . $_ * 5 . " >> $query.output";
print "\tRunning program_2 on query $query with input param $_...\n";
system($cmd_2); }
}
由于第一个程序通常比第二个程序完成得快,因此我认为有可能通过继续在program_2上运行新的查询(同时program_2也正在上一个查询上运行)来加快整个处理的速度。 加快速度太好了,因为目前需要很多小时才能完成处理。 但是,我不确定该怎么做。 像Parallel :: ForkManager这样的解决方案会解决吗? 或在Perl中使用线程?
现在,在我的实际代码中,我进行了一些错误处理,并为program_2设置了超时-我使用fork,exec和$ SIG {ALRM}来执行此操作,但我真的不知道自己在做什么。 我仍然有能力执行此操作很重要,否则program_2可能会卡住或无法充分报告失败原因。 这是带有错误处理的代码。 在可重现的示例中,我认为它没有达到应有的效果,但是至少您希望可以看到我正在尝试做的事情。 错误处理如下:
#!/usr/bin/perl
#
# stackoverflow_q_7-7-2016.pl
use warnings;
use strict;
my @queries_list = (2, 4, 3, 1);
foreach my $query (@queries_list) {
#Command meant to simulate the first, shorter process, and return a list of results for the next process
my $cmd_1 = "sleep " . $query . "s; shuf -i 4-15 -n 3";
print "Running program_1 on query $query...\n";
my @results = qx($cmd_1);
foreach (@results) {
chomp $_;
#Command meant to simulate a longer process whose input depends on program_1; the output I write to a separate file for each query
my $cmd_2 = "sleep " . $_ . "s; fortune -s | head -c " . $_ * 3 . " >> $query.output";
print "\tRunning program_2 on query $query with input param $_...\n";
my $childPid;
eval {
local $SIG{ALRM} = sub { die "Timed out" };
alarm 10;
if ($childPid = fork()) {
wait();
} else {
exec($cmd_2);
}
alarm 0;
};
if ($? != 0) {
my $exitCode = $? >> 8;
print "Program_2 exited with error code $exitCode. Retry...\n";
}
if ($@ =~ /Timed out/) {
print "\tProgram_2 timed out. Skipping...\n";
kill 2, $childPid;
wait;
};
}
}
感谢所有帮助。
一种解决方案:
use threads;
use Thread::Queue; # 3.01+
sub job1 { ... }
sub job2 { ... }
{
my $job1_request_queue = Thread::Queue->new();
my $job2_request_queue = Thread::Queue->new();
my $job1_thread = async {
while (my $job = $job1_request_queue->dequeue()) {
my $result = job1($job);
$job2_request_queue->enqueue($result);
}
$job2_request_queue->end();
};
my $job2_thread = async {
while (my $job = $job2_request_queue->dequeue()) {
job2($job);
}
};
$job1_request_queue->enqueue($_) for ...;
$job1_request_queue->end();
$_->join() for $job1_thread, $job2_thread;
}
您甚至可以同时拥有两种类型的多个工作器。
use threads;
use Thread::Queue; # 3.01+
use constant NUM_JOB1_WORKERS => 1;
use constant NUM_JOB2_WORKERS => 3;
sub job1 { ... }
sub job2 { ... }
{
my $job1_request_queue = Thread::Queue->new();
my $job2_request_queue = Thread::Queue->new();
my @job1_threads;
for (1..NUM_JOB1_WORKERS) {
push @job1_threads, async {
while (my $job = $job1_request_queue->dequeue()) {
my $result = job1($job);
$job2_request_queue->enqueue($result);
}
};
}
my @job2_threads;
for (1..NUM_JOB2_WORKERS) {
push @job2_threads, async {
while (my $job = $job2_request_queue->dequeue()) {
job2($job);
}
};
}
$job1_request_queue->enqueue($_) for ...;
$job1_request_queue->end();
$_->join() for @job1_threads;
$job2_request_queue->end();
$_->join() for @job2_threads;
}
使用IPC :: Run而不是qx
添加超时。 无需信号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.