在Windows上同时调用perl系统

时间:2012-09-11 19:22:12

标签: perl

尝试找到一种方法让perl脚本在Windows上运行4个其他perl脚本,然后一旦完成,就启动第5个脚本。我已经检查了一堆东西,但似乎都不是直截了当的。建议欢迎。这些脚本将在Windows机器上运行。脚本1-4需要在开始脚本5之前先完成

3 个答案:

答案 0 :(得分:0)

  1. 接受其他问题的答案

  2. use threads

    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。等待completition

    $_->join() foreach @jobs;
    

    2.3。启动最后一个脚本

  3. 修改

    当你表示我的解决方案对你不起作用时,我启动了我的Windoze盒子,教我使用这个可怕的cmd.exe并编写了以下测试脚本。它比上述解决方案略微简化,但 满足您对顺序等的要求。

    #!/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上使用Strawberry Perl v5.12.2):

    C:\...>perl stackoverflow.pl
    

    这是输出:

    "script 1 reporting"
    "script 3 reporting"
    hi there! This is script 2 reporting
    finished all my jobs
    "This is the last job"
    

    那么这究竟是怎么回事呢?我非常想学习下次在非GNU系统上编写脚本时绕过Perl的陷阱,所以请告诉我可能出错的地方。

答案 1 :(得分:0)

根据个人经验,在ActiveState中使用fork() Perl并不总是按顺序运行该进程。在那里使用的fork()的线程模拟似乎启动了所有进程,将其运行到某一点,然后一次运行一个进程。这甚至适用于多核CPU。我认为Strawberry Perl的编译方式是一样的。另外,请注意fork()仍然被用于反叛和system(),它只是抽象而已。

如果你在Windows上使用Cygwin Perl,它将通过Cygwin自己的fork()调用运行,并且事情将正确并行化。但是,Cygwin在其他方面比较慢。

答案 2 :(得分:0)

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`;