Skip to content

Instantly share code, notes, and snippets.

@meso-cacase
Created November 8, 2012 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meso-cacase/4037338 to your computer and use it in GitHub Desktop.
Save meso-cacase/4037338 to your computer and use it in GitHub Desktop.
ファイルまたは標準入力からコマンドリストを読み込み並列実行
#!/usr/bin/perl
# ファイルまたは標準入力からコマンドリストを読み込み並列実行する
#
# Usage: ./parallel.pl [-MAX_PROC] commandlist.txt
#
# MAX_PROC は並列実行するコマンドの最大数(数値)
# 省略時は 1、つまり並列化せず1行ずつ順番に実行
# コマンドリストは1行に1コマンドを記載
#
# 2012-11-08.@meso_cacase
use warnings ;
use strict ;
$| = 1 ;
# 並列実行するコマンドの最大数
my $proc_max = ($ARGV[0] =~ s/^-(\d+)$/$1/) ?
shift @ARGV : # 第1引数に指定されている場合はその値
1 ; # 省略時は 1、つまり並列化せず1行ずつ順番に実行
# コマンドリストを読み込む
my @command_list = <> ;
parallel(\@command_list, $proc_max) ;
exit ;
# ====================
sub parallel { # コマンドリストを読み込み並列実行する
my @command_list = @{$_[0]} ; # コマンドリストをリファレンスで与える
my $proc_max = $_[1] || 1 ; # 並列実行するコマンドの最大数
foreach (@command_list){
chomp ;
if (my $pid = fork){
# 親プロセス
print "$pid $_\n" ;
$proc_max -- ;
} else {
# 子プロセス
system $_ ;
exit ;
}
# 並列実行するコマンドの最大数に達したらwaitする
unless ($proc_max){
wait ;
$proc_max ++ ;
}
}
# 子プロセスが無くなる、つまりwaitが-1を返すまでwaitする
while (wait != -1){ }
} ;
# ====================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment