Perl Parallel :: ForkManager Wait_All_Children ()은 지나치게 오랜 시간이 걸립니다.

StackOverflow https://stackoverflow.com/questions/2428690

문제

사용하는 스크립트가 있습니다 Parallel::ForkManager. 그러나 Wait_all_children () 프로세스는 모든 어린이 프로세스가 완료된 후에도 엄청나게 오래 걸립니다. 내가 아는 방식은 일부 타임 스탬프를 인쇄하는 것입니다 (아래 참조). 누구 든지이 원인이 무엇인지 아는 사람이 있습니까 (내 컴퓨터에 16 개의 CPU 코어가 있습니다)?

my $pm = Parallel::ForkManager->new(16);
for my $i (1..16) {
    $pm->start($i) and next;

    ... do something within the child-process ...

    print (scalar localtime), " Process $i completed.\n";
    $pm->finish();
}
print (scalar localtime), " Waiting for some child process to finish.\n"; 
$pm->wait_all_children();
print (scalar localtime), " All processes finished.\n"; 

분명히, 나는 얻을 것이다 Waiting for some child process to finish 먼저, Timestamp가있는 메시지, 7:08:35. 그런 다음 목록을 얻을 수 있습니다 Process i completed 마지막 메시지가 있습니다 7:10:30. 그러나 나는 메시지를받지 못한다 All Processes finished ~까지 7:16:33(!). 왜 6 분 지연이 7:10:30과 7:16:33 사이입니까? 고마워!

도움이 되었습니까?

해결책

나는 이것을 시도했다 :

#!/opt/perl/bin/perl

use strict; use warnings;

use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(16);

for my $i (1..16) {
    $pm->start($i) and next;
    sleep rand 20;
    printf "%s : Process %d completed\n", scalar localtime, $i;
    $pm->finish;
}

printf "%s: Waiting for some child to finish\n", scalar localtime;
$pm->wait_all_children;

printf "%s: All processes finished.\n", scalar localtime; 

나는 얻었다 :

[sinan@archardy Src]$ ./y.pl
Thu Mar 11 17:14:16 2010 : Process 3 completed
Thu Mar 11 17:14:16 2010: Waiting for some child to finish
Thu Mar 11 17:14:18 2010 : Process 8 completed
Thu Mar 11 17:14:18 2010 : Process 14 completed
<snip>...</snip>
Thu Mar 11 17:14:34 2010 : Process 12 completed
Thu Mar 11 17:14:34 2010: All processes finished.

나는 가지고있다 perl 5.10.1 Linux에서 병렬 :: 포크 매너 버전 0.7.5.

그러므로 나는 당신이 가질 때 어떤 문제가 일어나고 있는지 결론을 내릴 때

# ... do something within the child-process ...

업데이트: 문제는 인쇄한다는 것입니다 Process finished message 앞에 finish 전화. 다음 버전을 시도하십시오.

#!/opt/perl/bin/perl

use strict; use warnings;

use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(16);
$pm->run_on_finish( sub {
    printf "%s : Process completed: @_\n", scalar localtime
});

for my $i (1..16) {
    $pm->start($i) and next;
    sleep rand 20;
    $pm->finish;
}

printf "%s: Waiting for some child to finish\n", scalar localtime;
$pm->wait_all_children;

printf "%s: All processes finished.\n", scalar localtime;

보다 병렬 콜백 :: 포크 메너 문서 자세한 내용은. 지연이 사라지면 관찰중인 증상은 포크 과정이 완료되기 전에 완료되었다고 주장했다는 사실 때문입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top