我只是想在後臺打開一個壓縮/未壓縮文件,並根據壓縮文件上的處理生成一個新文件。如何從子處理中分叉後臺進程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);
}
我的問題:
- 確實在
backgroundProcess
創建子進程即使在return
發生後仍然運行(在行return if $pid
? - in
process_file
,我如何確保爲每個文件打開一個唯一的文件句柄,還是將「fork」處理掉? - 在環(通過
@filesToProcess
去),我想一次運行只一定數量的過程,讓我怎麼檢查,如果後臺進程的數目等於$LIMIT
,然後打開一個新的一個老的完成?
如果你不想使用CPAN模塊,我建議http://stackoverflow.com/questions/17155204/lightweight-fork-replacement-for-threads'我的$叉= {FASYNC「不解壓」}; $叉 - >();' – 2014-09-24 14:25:58
使用'並行:: ForkManager'。 *「那是不可用的」*是什麼意思? – Borodin 2014-09-24 14:30:06
另請勿使用'&'加前綴。這曾經是必要的,但現在是多餘的最好 - 在最壞的情況打破了微妙的方式事情。 – Sobrique 2014-09-24 14:39:10