windows上的并行perl系统调用
试图找到一种方法让一个perl脚本在windows上运行另外4个perl脚本,然后在完成所有操作后,启动第5个脚本。我查了很多东西,但没有一件是直截了当的。欢迎提出建议。这些脚本将在windows设备上运行。在启动脚本5之前,脚本1-4需要先完成windows上的并行perl系统调用,perl,Perl,试图找到一种方法让一个perl脚本在windows上运行另外4个perl脚本,然后在完成所有操作后,启动第5个脚本。我查了很多东西,但没有一件是直截了当的。欢迎提出建议。这些脚本将在windows设备上运行。在启动脚本5之前,脚本1-4需要先完成 接受其他问题的答案 使用线程 2.1。4个脚本的基调: my @scripts = qw(... commands ...); my @jobs = (); foreach my $script (@scripts) { my $job = thr
使用线程
2.1。4个脚本的基调:
my @scripts = qw(... commands ...);
my @jobs = ();
foreach my $script (@scripts) {
my $job = threads->create( sub {
system($script);
});
push @jobs, $job;
}
2.2。等待完成
$_->join() foreach @jobs;
2.3。开始最后一个脚本#!/usr/bin/perl
use strict; use warnings; use threads;
my @scripts = (
q(echo "script 1 reporting"),
q(perl -e "sleep 2; print qq{hi there! This is script 2 reporting\n}"),
q(echo "script 3 reporting"),
);
my @jobs = map {
threads->create(sub{
system($_);
});
} @scripts;
$_->join foreach @jobs;
print "finished all my jobs\n";
system q(echo "This is the last job");
我使用此命令执行脚本(在Win7上,使用草莓Perl v5.12.2):
这是输出:
"script 1 reporting"
"script 3 reporting"
hi there! This is script 2 reporting
finished all my jobs
"This is the last job"
那么,这究竟是怎么不起作用的呢?下次在非GNU系统上编写脚本时,我非常想学习如何绕过Perl的陷阱,因此请告诉我可能出现的错误。根据个人经验,在ActiveState中使用
fork()
,Perl并不总是按顺序运行进程。这里使用的fork()
的线程模拟似乎启动了所有进程,将其运行到某一点,然后一次运行一个进程。这甚至适用于多核CPU。我认为草莓Perl也是以同样的方式编译的。另外,请记住,fork()
仍被用于反勾号,而system()
,它只是抽象出来的
如果在Windows上使用Cygwin Perl,它将通过Cygwin自己的
fork()
调用运行,事情将正确地并行化。但是,Cygwin在其他方面速度较慢。调度由操作系统完成,而不是由Perl完成。是的,ActiveState和草莓使用相同的模拟。@ikegami:效果从何而来并不重要——这是一个需要注意的问题。这不起作用,但谢谢。我不知道你在说什么,接受答案。我问过的大多数问题都没有答案,或者答案对我不适用。我将更积极地响应所提供的帮助,并接受有效的答案。@user1279586很抱歉听到这个消息,很遗憾我无法帮助您使用Ruby…好吧,我的代码对我来说很有效,Perl应该隐藏这样一个事实,即我们正在使用不同的操作系统和如此简单的脚本。我写了一个Windoze测试,效果很好。你能比较一下你的结果吗?
use Proc::Background;
my @commands = (
['./Files1.exe ALL'],
['./Files2.exe ALL'],
['./Files3.exe ALL'],
['./Files4.exe ALL'],
);
my @procs = map { Proc::Background->new(@$_) } @commands;
$_->wait for @procs;
system 'echo', 'CSCProc', '--pidsAndExitStatus', map { $_->pid, $_->wait } @procs;
`mergefiles.exe`;
use Proc::Background;
my @commands = (
['./Files1.exe ALL'],
['./Files2.exe ALL'],
['./Files3.exe ALL'],
['./Files4.exe ALL'],
);
my @procs = map { Proc::Background->new(@$_) } @commands;
$_->wait for @procs;
system 'echo', 'CSCProc', '--pidsAndExitStatus', map { $_->pid, $_->wait } @procs;
`mergefiles.exe`;