2014-09-24 25 views
0

我只是想在後臺打開一個壓縮/未壓縮文件,並根據壓縮文件上的處理生成一個新文件。如何從子處理中分叉後臺進程1000s文件

我可以做到這一點Parallel::ForkManager,但我認爲不可用。

我發現了這一點,但我不知道如何使用它:

sub backgroundProcess { 
    my $file = shift; 
    my $pid = fork; 
    return if $pid; # in the parent process 
    &process_file($file); 
    exit;    # end child process 
} 

sub process_file { 
    my $file = shift; 
    my $outFile = $file . ".out"; 
    # ...here... 
    open(readHandle, "<", $file) or die print "failed $!"; 
    open(writeHandle, ">", $outFile) or die "failed write $!"; 
    # some processing here..... 
    # and then closing handles... 
} 

循環:

foreach my $file (@filesToProcess) { 
    &backgroundProcess($file); 
} 

我的問題:

  1. 確實在backgroundProcess創建子進程即使在return發生後仍然運行(在行return if $pid
  2. in process_file,我如何確保爲每個文件打開一個唯一的文件句柄,還是將「fork」處理掉?
  3. 在環(通過@filesToProcess去),我想一次運行一定數量的過程,讓我怎麼檢查,如果後臺進程的數目等於$LIMIT,然後打開一個新的一個老的完成?
+0

如果你不想使用CPAN模塊,我建議http://stackoverflow.com/questions/17155204/lightweight-fork-replacement-for-threads'我的$叉= {FASYNC「不解壓」}; $叉 - >();' – 2014-09-24 14:25:58

+2

使用'並行:: ForkManager'。 *「那是不可用的」*是什麼意思? – Borodin 2014-09-24 14:30:06

+2

另請勿使用'&'加前綴。這曾經是必要的,但現在是多餘的最好 - 在最壞的情況打破了微妙的方式事情。 – Sobrique 2014-09-24 14:39:10

回答

3

如果我瞭解您的問題的標題,您正在尋找Parallel::ForkManager

我不明白爲什麼Parallel::ForkManager不可用。它是一個純粹的Perl模塊。

use Parallel::ForkManager; 

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

for my $file (@filesToProcess) { 
    # Forks and returns the pid for the child: 
    my $pid = $pm->start and next; 

    ... do some work with $data in the child process ... 

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

您可以將模塊的.pm文件複製到您可以找到的位置。例如:

/some/custom/path/myscript 
/some/custom/path/inc/Parallel/Forkmanager.pm

然後,在myscript

use FindBin qw($RealBin); 
use lib "$RealBin/inc"; 
use Parallel::ForkManager; 

,當然,如果由於某種深不可測的原因,你不能做到這一點,你可以隨時fatpack你的腳本。

+0

長壽命叉子與父母分享數據的最佳方式是什麼? – 2014-09-24 14:41:18

+0

並不是每個網站都允許從互聯網上下載和安裝ad-hoc的東西。和CPAN一樣,我也很喜歡它,所以我仍然很樂意將它部署在我的某些生產服務器上。 – Sobrique 2014-09-24 14:42:27

+0

@Sobrique它應該不是一個問題,如果這些都是純Perl模塊? – 2014-09-24 14:43:54

0

Re Q1:是的。只有父進程纔會執行返回,因爲$ pid在子進程中將爲零。

問題二:不知道我是否正確理解你的問題。 open()將在子進程中執行,因此文件句柄對於子進程是本地的。

Re Q3:您必須手動進行跟蹤。達到限制後,在啓動新的子進程之前調用wait()等待一個子進程退出。請參閱http://perldoc.perl.org/functions/wait.html