2015-01-11 7 views
1

두 가지 방법으로 확인했지만 성공하지 못한 것 같습니다.두 개 이상의 Perl 프로그램을 병렬로 실행하는 방법 - 두 개 이상의 .pl 파일 사용

두 펄 파일 Parallel::ForkManager

를 사용

pro_1.pl

use strict; 

for (1..10) { 
    print "finite for one\n"; 
} 

pro_2.pl

use strict; 

for (1..10){ 
    print "finite for two\n"; 
} 

사례 1, 아래의 경우에 사용되는,

use strict; 
use warnings; 

use Parallel::ForkManager; 

my $pm = new Parallel::ForkManager(2); 
my @all_pro = ("pro_1.pl", "pro_2.pl"); 

foreach my $pro (@all_pro) { 
    # Forks and returns the pid for the child: 
    my $pid = $pm->start and next; 
    system ("perl $pro"); 

    $pm->finish; # Terminates the child process 
    } 

출력 : 두 개의 서로 다른 프로그램에서 인쇄 문 사이에 인터리브가 없습니다

$ finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 

. 두 경우 모두에 대한

$ finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for one 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 
finite for two 

출력은 동일 : 펄을 사용하여

사례 2

use threads; 

my $child_thread = threads->new(\&my_function1, "pro_1.pl"); 
my $child_thread2 = threads->new(\&my_function2, "pro_2.pl"); 
# ... 

my $this_thread = threads->self; 
print "Main thread: $this_thread\n"; 
my $tid = $this_thread->tid; 
print "TID of current thread: $tid\n"; 

my @threads = threads->list; 
print "All threads: @threads\n"; 

foreach my $thr (@threads) { 
    my $thr_pid = $thr->tid; 
    print "Child Thread PID: $thr_pid\n"; 
    $thr->join; 
} 

sub my_function1 { 
    system ("perl @_"); 
} 

sub my_function2 { 
    system ("perl @_"); 
} 

출력 스레드. 이 경우 실제로 첫 번째 프로그램이 완료되면 두 번째 프로그램이 실행됩니다. 진정한 병렬 처리가 아닙니다. 서브 루틴에서 실행하면 병렬 처리가 수행됩니다. 즉, "2에 대해 유한"문과 "1에 대해 유한"문이 인터리브 된 것을 볼 수 있습니다.

+0

많은 핵심 엔진 인 MCE를 살펴보십시오. CPAN 모듈. –

답변

4

pro_1 및 pro_2 프로그램에 거의 시간이 걸리지 않으므로 병렬 처리가 표시되지 않습니다.

sleep 1;을 루프에 배치하면 출력이 인터리브됩니다. foreach my $pro 루프 이후에 $pm->wait_all_children을 수행하기를 원할 수도 있으므로 모든 프로그램이 끝날 때까지 주 프로그램이 계속됩니다.

관련 문제