1 |
ktats |
1.1 |
=encoding utf8 |
2 |
|
|
|
3 |
|
|
=head1 名前 |
4 |
|
|
|
5 |
|
|
=begin original |
6 |
|
|
|
7 |
|
|
Parallel::ForkManager - A simple parallel processing fork manager |
8 |
|
|
|
9 |
|
|
=end original |
10 |
|
|
|
11 |
|
|
Parallel::ForkManager - 簡単な並列処理によるforkマネージャー |
12 |
|
|
|
13 |
|
|
=head1 概要 |
14 |
|
|
|
15 |
|
|
use Parallel::ForkManager; |
16 |
|
|
|
17 |
|
|
$pm = new Parallel::ForkManager($MAX_PROCESSES); |
18 |
|
|
|
19 |
|
|
foreach $data (@all_data) { |
20 |
|
|
# Forks and returns the pid for the child: |
21 |
|
|
my $pid = $pm->start and next; |
22 |
|
|
|
23 |
|
|
... do some work with $data in the child process ... |
24 |
|
|
... 子プロセスにより$dataに関するいくつかの処理を行う ... |
25 |
|
|
|
26 |
|
|
$pm->finish; # Terminates the child process |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
=head1 説明 |
30 |
|
|
|
31 |
|
|
=begin original |
32 |
|
|
|
33 |
|
|
This module is intended for use in operations that can be done in parallel |
34 |
|
|
where the number of processes to be forked off should be limited. Typical |
35 |
|
|
use is a downloader which will be retrieving hundreds/thousands of files. |
36 |
|
|
|
37 |
|
|
=end original |
38 |
|
|
|
39 |
|
|
このモジュールは稼動中において並列処理されることを意図している. |
40 |
|
|
その際,プロセスの分岐数は制限されるべきものである. |
41 |
|
|
典型的な使われ方としてはファイルの数百/数千を取得するダウンローダーとしてである. |
42 |
|
|
|
43 |
|
|
=begin original |
44 |
|
|
|
45 |
|
|
The code for a downloader would look something like this: |
46 |
|
|
|
47 |
|
|
=end original |
48 |
|
|
|
49 |
|
|
ダウンローダーのコードとしてはこのようになるでしょう: |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
use LWP::Simple; |
53 |
|
|
use Parallel::ForkManager; |
54 |
|
|
|
55 |
|
|
... |
56 |
|
|
|
57 |
|
|
@links=( |
58 |
|
|
["http://www.foo.bar/rulez.data","rulez_data.txt"], |
59 |
|
|
["http://new.host/more_data.doc","more_data.doc"], |
60 |
|
|
... |
61 |
|
|
); |
62 |
|
|
|
63 |
|
|
... |
64 |
|
|
|
65 |
|
|
# Max 30 processes for parallel download |
66 |
|
|
# 最大30プロセスで並列的にダウンロードを行います |
67 |
|
|
my $pm = new Parallel::ForkManager(30); |
68 |
|
|
|
69 |
|
|
foreach my $linkarray (@links) { |
70 |
|
|
$pm->start and next; # do the fork |
71 |
|
|
# forkさせます |
72 |
|
|
|
73 |
|
|
my ($link,$fn) = @$linkarray; |
74 |
|
|
warn "Cannot get $fn from $link" |
75 |
|
|
if getstore($link,$fn) != RC_OK; |
76 |
|
|
|
77 |
|
|
$pm->finish; # do the exit in the child process |
78 |
|
|
# 子プロセスをexitします |
79 |
|
|
} |
80 |
|
|
$pm->wait_all_children; |
81 |
|
|
|
82 |
|
|
=begin original |
83 |
|
|
|
84 |
|
|
First you need to instantiate the ForkManager with the "new" constructor. |
85 |
|
|
You must specify the maximum number of processes to be created. If you |
86 |
|
|
specify 0, then NO fork will be done; this is good for debugging purposes. |
87 |
|
|
|
88 |
|
|
=end original |
89 |
|
|
|
90 |
|
|
まず初めにForkManagerの"new"コンストラクタを具体化する必要があります. |
91 |
|
|
コンストラクタを作成する際に,プロセス数の最大値を指定しなければなりません. |
92 |
|
|
もしプロセス数を0と指定した場合はforkはされないでしょう;これはデバッグを行う為のよい方法です. |
93 |
|
|
|
94 |
|
|
=begin original |
95 |
|
|
|
96 |
|
|
Next, use $pm->start to do the fork. $pm returns 0 for the child process, |
97 |
|
|
and child pid for the parent process (see also L<perlfunc(1p)/fork()>). |
98 |
|
|
The "and next" skips the internal loop in the parent process. NOTE: |
99 |
|
|
$pm->start dies if the fork fails. |
100 |
|
|
|
101 |
|
|
=end original |
102 |
|
|
|
103 |
|
|
次に,forkする為に$pm->startを使います. $pmは子プロセスの為に0にを返し, |
104 |
|
|
親プロセスの為に子pidを返します (または, L<perlfunc(1p)/fork()>を見て下さい). |
105 |
|
|
"and next"は親プロセスにおける内部の繰り返しをスキップします. 注意: |
106 |
|
|
$pm->startはforkが失敗すれば死にます. |
107 |
|
|
|
108 |
|
|
=begin original |
109 |
|
|
|
110 |
|
|
$pm->finish terminates the child process (assuming a fork was done in the |
111 |
|
|
"start"). |
112 |
|
|
|
113 |
|
|
=end original |
114 |
|
|
|
115 |
|
|
$pm->finishは子プロセスを終了させます ("start"でforkを開始したと仮定した場合). |
116 |
|
|
|
117 |
|
|
=begin original |
118 |
|
|
|
119 |
|
|
NOTE: You cannot use $pm->start if you are already in the child process. |
120 |
|
|
If you want to manage another set of subprocesses in the child process, |
121 |
|
|
you must instantiate another Parallel::ForkManager object! |
122 |
|
|
|
123 |
|
|
=end original |
124 |
|
|
|
125 |
|
|
注意:あなたがすでに子プロセスに居るのならば,$pm->startを使うことはできません. |
126 |
|
|
子プロセスをもう1セット,サブプロセスとして管理したいのならば, |
127 |
|
|
別のParallel::ForkManagerオブジェクトを具体化する必要があります. |
128 |
|
|
|
129 |
|
|
=head1 メソッド |
130 |
|
|
|
131 |
|
|
=begin original |
132 |
|
|
|
133 |
|
|
The comment letter indicates where the method should be run. P for parent, |
134 |
|
|
C for child. |
135 |
|
|
|
136 |
|
|
=end original |
137 |
|
|
|
138 |
|
|
後ろのコメントはどこでメソッドを使うかを示しています.P は親プロセスで, |
139 |
|
|
Cが子プロセスです. |
140 |
|
|
|
141 |
|
|
=over 5 |
142 |
|
|
|
143 |
|
|
=item new $processes [, $tempdir] # P |
144 |
|
|
|
145 |
|
|
=begin original |
146 |
|
|
|
147 |
|
|
Instantiate a new Parallel::ForkManager object. You must specify the maximum |
148 |
|
|
number of children to fork off. If you specify 0 (zero), then no children |
149 |
|
|
will be forked. This is intended for debugging purposes. |
150 |
|
|
|
151 |
|
|
=end original |
152 |
|
|
|
153 |
|
|
新しいParallel::ForkManagerオブジェクトを具体化します. |
154 |
|
|
子プロセスを分岐させる最大数を指定しなければなりません. |
155 |
|
|
0を指定した場合は分岐される子はありません. |
156 |
|
|
これはデバッグ目的のために意図されます. |
157 |
|
|
|
158 |
|
|
=begin original |
159 |
|
|
|
160 |
|
|
The optional second parameter, $tempdir, is only used if you want the |
161 |
|
|
children to send back a reference to some data (see RETRIEVING DATASTRUCTURES |
162 |
|
|
below). If not provided, it is set to $L<File::Spec>->tmpdir(). |
163 |
|
|
|
164 |
|
|
=end original |
165 |
|
|
|
166 |
|
|
オプションの二番目のパラメータ$tempdirは,子プロセスから何らかのデータをリファレンスとして |
167 |
|
|
戻したい場合にのみ使います(後述のRETRIEVING DATASTRUCTURES を参照). |
168 |
|
|
与えられなければ,$L<File::Spec>->tmpdir() をセットします. |
169 |
|
|
|
170 |
|
|
=begin original |
171 |
|
|
|
172 |
|
|
The new method will die if the temporary directory does not exist or it is not |
173 |
|
|
a directory, whether you provided this parameter or the |
174 |
|
|
$L<File::Spec>->tmpdir() is used. |
175 |
|
|
|
176 |
|
|
=end original |
177 |
|
|
|
178 |
|
|
new メソッドはテンポラリディレクトリが存在しないか,それがディレクトリでなければ死にます. |
179 |
|
|
パラメタを渡しても,$L<File::Spec>->tmpdir()が使われても同じです. |
180 |
|
|
|
181 |
|
|
=item start [ $process_identifier ] # P |
182 |
|
|
|
183 |
ktats |
1.2 |
=begin original |
184 |
|
|
|
185 |
ktats |
1.1 |
This method does the fork. It returns the pid of the child process for |
186 |
|
|
the parent, and 0 for the child process. If the $processes parameter |
187 |
|
|
for the constructor is 0 then, assuming you're in the child process, |
188 |
|
|
$pm->start simply returns 0. |
189 |
|
|
|
190 |
|
|
=end original |
191 |
|
|
|
192 |
|
|
このメソッドはforkを実行します. |
193 |
|
|
子プロセスのpidを親プロセスに返し,子プロセスには0を返します. |
194 |
|
|
$processesパラメータがコンストラクタに0として渡された場合, |
195 |
|
|
あなたがすでに子プロセスに居ると仮定され, |
196 |
|
|
$pm->startは単に0を返します. |
197 |
|
|
|
198 |
|
|
=begin original |
199 |
|
|
|
200 |
|
|
An optional $process_identifier can be provided to this method... It is used by |
201 |
|
|
the "run_on_finish" callback (see CALLBACKS) for identifying the finished |
202 |
|
|
process. |
203 |
|
|
|
204 |
|
|
=end original |
205 |
|
|
|
206 |
|
|
任意で$process_identifier(プロセス識別子)をメソッドに提供することができます... |
207 |
|
|
これは"run_on_finish"で終了したプロセスを特定し回収する為に使われます. |
208 |
|
|
(CALLBACKSの項目を見て下さい) |
209 |
|
|
|
210 |
|
|
=item finish [ $exit_code [, $data_structure_reference] ] # C |
211 |
|
|
|
212 |
|
|
=begin original |
213 |
|
|
|
214 |
|
|
Closes the child process by exiting and accepts an optional exit code |
215 |
|
|
(default exit code is 0) which can be retrieved in the parent via callback. |
216 |
|
|
If the second optional parameter is provided, the child attempts to send |
217 |
|
|
it's contents back to the parent. If you use the program in debug mode |
218 |
|
|
($processes == 0), this method just calls the callback. |
219 |
|
|
|
220 |
|
|
=end original |
221 |
|
|
|
222 |
|
|
exit で子プロセスを閉じ,オプションとして終了コードを取ります(規定の 終了コードは 0 です). |
223 |
|
|
終了コードは,コールバック経由で親プロセスが取得できます. |
224 |
|
|
2番目のパラメータを渡した場合,子プロセスは親プロセスにその内容を送ろうとします. |
225 |
|
|
このプログラムをデバッグモードで使用する場合 ($processes == 0 の場合), |
226 |
|
|
このメソッドは単にコールバックを呼びます. |
227 |
|
|
|
228 |
|
|
=begin original |
229 |
|
|
|
230 |
|
|
If the $data_structure_reference is provided, then it is serialized and |
231 |
|
|
passed to the parent process. See RETRIEVING DATASTRUCTURES for more info. |
232 |
|
|
|
233 |
|
|
=end original |
234 |
|
|
|
235 |
|
|
$data_structure_reference が渡された場合は,シリアライズされて親プロセスに渡されます. |
236 |
|
|
詳細はRETRIEVING DATASTRUCTURES を見てください. |
237 |
|
|
|
238 |
|
|
=item set_max_procs $processes # P |
239 |
|
|
|
240 |
|
|
=begin original |
241 |
|
|
|
242 |
|
|
Allows you to set a new maximum number of children to maintain. |
243 |
|
|
|
244 |
|
|
=end original |
245 |
|
|
|
246 |
|
|
新しく子プロセス数の最大値を設定することができます. |
247 |
|
|
|
248 |
|
|
=item wait_all_children # P |
249 |
|
|
|
250 |
|
|
=begin original |
251 |
|
|
|
252 |
|
|
You can call this method to wait for all the processes which have been |
253 |
|
|
forked. This is a blocking wait. |
254 |
|
|
|
255 |
|
|
=end original |
256 |
|
|
|
257 |
|
|
forkされた全ての子プロセスを待つ為にこのメソッドを呼ぶことができます. |
258 |
|
|
これはブロッキングする wait です. |
259 |
|
|
|
260 |
|
|
=back |
261 |
|
|
|
262 |
|
|
=head1 コールバック |
263 |
|
|
|
264 |
|
|
=begin original |
265 |
|
|
|
266 |
|
|
You can define callbacks in the code, which are called on events like starting |
267 |
|
|
a process or upon finish. Declare these before the first call to start(). |
268 |
|
|
|
269 |
|
|
=end original |
270 |
|
|
|
271 |
|
|
プロセスの開始時または終了時のイベントで呼ばれるコールバックを定義することができます. |
272 |
|
|
これらは最初のstart()の呼び出しより前に定義してください. |
273 |
|
|
|
274 |
|
|
=begin original |
275 |
|
|
|
276 |
|
|
The callbacks can be defined with the following methods: |
277 |
|
|
|
278 |
|
|
=end original |
279 |
|
|
|
280 |
|
|
コールバックは以下のメソッドで定義することができます: |
281 |
|
|
|
282 |
|
|
=over 4 |
283 |
|
|
|
284 |
|
|
=item run_on_finish $code [, $pid ] # P |
285 |
|
|
|
286 |
|
|
=begin original |
287 |
|
|
|
288 |
|
|
You can define a subroutine which is called when a child is terminated. It is |
289 |
|
|
called in the parent process. |
290 |
|
|
|
291 |
|
|
=end original |
292 |
|
|
|
293 |
|
|
子プロセスが終了する際に呼ばれるサブルーチンを定義することができます. |
294 |
|
|
これは親プロセスから呼ばれます. |
295 |
|
|
|
296 |
|
|
=begin original |
297 |
|
|
|
298 |
|
|
The paremeters of the $code are the following: |
299 |
|
|
|
300 |
|
|
=end original |
301 |
|
|
|
302 |
|
|
$codeのパラメータは以下です: |
303 |
|
|
|
304 |
|
|
=begin original |
305 |
|
|
|
306 |
|
|
- pid of the process, which is terminated |
307 |
|
|
- exit code of the program |
308 |
|
|
- identification of the process (if provided in the "start" method) |
309 |
|
|
- exit signal (0-127: signal name) |
310 |
|
|
- core dump (1 if there was core dump at exit) |
311 |
|
|
- datastructure reference or undef (see RETRIEVING DATASTRUCTURES) |
312 |
|
|
|
313 |
|
|
=end original |
314 |
|
|
|
315 |
|
|
- 終了するプロセスのpid |
316 |
|
|
- プログラムの終了コード |
317 |
|
|
- プロセスの識別 ("start"メソッドで提供される場合) |
318 |
|
|
- 終了シグナル (0-127: シグナル名) |
319 |
|
|
- コアダンプ (1 はコアダンプによる終了です) |
320 |
|
|
- データ構造のリファレンスundef (RETRIEVING DATASTRUCTURES 参照) |
321 |
|
|
|
322 |
|
|
=item run_on_start $code # P |
323 |
|
|
|
324 |
|
|
=begin original |
325 |
|
|
|
326 |
|
|
You can define a subroutine which is called when a child is started. It called |
327 |
|
|
after the successful startup of a child in the parent process. |
328 |
|
|
|
329 |
|
|
=end original |
330 |
|
|
|
331 |
|
|
子プロセスが開始する際に呼ばれるサブルーチンを定義することができます. |
332 |
|
|
親プロセスの中で子プロセスが正常に開始された後に呼ばれます. |
333 |
|
|
|
334 |
|
|
=begin original |
335 |
|
|
|
336 |
|
|
The parameters of the $code are the following: |
337 |
|
|
|
338 |
|
|
=end original |
339 |
|
|
|
340 |
|
|
$codeのパラメータは以下です: |
341 |
|
|
|
342 |
|
|
=begin original |
343 |
|
|
|
344 |
|
|
- pid of the process which has been started |
345 |
|
|
- identification of the process (if provided in the "start" method) |
346 |
|
|
|
347 |
|
|
=end original |
348 |
|
|
|
349 |
|
|
- 開始されたプロセスのpid |
350 |
|
|
- プロセスの識別 ("start"メソッドで提供される場合) |
351 |
|
|
|
352 |
|
|
=item run_on_wait $code, [$period] # P |
353 |
|
|
|
354 |
|
|
=begin original |
355 |
|
|
|
356 |
|
|
You can define a subroutine which is called when the child process needs to wait |
357 |
|
|
for the startup. If $period is not defined, then one call is done per |
358 |
|
|
child. If $period is defined, then $code is called periodically and the |
359 |
|
|
module waits for $period seconds betwen the two calls. Note, $period can be |
360 |
|
|
fractional number also. The exact "$period seconds" is not guarranteed, |
361 |
|
|
signals can shorten and the process scheduler can make it longer (on busy |
362 |
|
|
systems). |
363 |
|
|
|
364 |
|
|
=end original |
365 |
|
|
|
366 |
|
|
子プロセスが開始時に待ちを必要とする場合に呼ばれるサブルーチンを定義することができます. |
367 |
|
|
$periodが定義されていない場合,一つの子プロセス単位で呼びます. |
368 |
|
|
$periodが定義されている場合,$codeは定期的に呼ばれ,モジュールは$period秒の間に |
369 |
|
|
2度呼ばれるのをモジュールは待ちます |
370 |
|
|
注意,$periodは断片的な数であるかもしれません. |
371 |
|
|
正確な"$period seconds"であることは保障しません, |
372 |
|
|
シグナルは短縮することができ,プロセススケジューラーでは長くすることができます (忙しいシステムでは). |
373 |
|
|
|
374 |
|
|
=begin original |
375 |
|
|
|
376 |
|
|
The $code called in the "start" and the "wait_all_children" method also. |
377 |
|
|
|
378 |
|
|
=end original |
379 |
|
|
|
380 |
|
|
$codeは"start"や"wait_all_children"メソッドにより呼ぶことができる. |
381 |
|
|
|
382 |
|
|
=begin original |
383 |
|
|
|
384 |
|
|
No parameters are passed to the $code on the call. |
385 |
|
|
|
386 |
|
|
=end original |
387 |
|
|
|
388 |
|
|
パラメータがない場合は呼び出しの際に$codeはパスすることができる. |
389 |
|
|
|
390 |
|
|
=back |
391 |
|
|
|
392 |
|
|
=head1 例 |
393 |
|
|
|
394 |
|
|
=head1 RETRIEVING DATASTRUCTURES from child processes |
395 |
|
|
|
396 |
|
|
=begin original |
397 |
|
|
|
398 |
|
|
The ability for the parent to retrieve data structures is new as of version |
399 |
|
|
0.7.6. |
400 |
|
|
|
401 |
|
|
=end original |
402 |
|
|
|
403 |
|
|
親プロセスでデータ構造を取得する機能は0.7.6の新機能です. |
404 |
|
|
|
405 |
|
|
=begin original |
406 |
|
|
|
407 |
|
|
Each child process may optionally send 1 data structure back to the parent. |
408 |
|
|
By data structure, we mean a reference to a string, hash or array. The |
409 |
|
|
contents of the data structure are written out to temporary files on disc |
410 |
|
|
using the L<Storable> modules' store() method. The reference is then |
411 |
|
|
retrieved from within the code you send to the run_on_finish callback. |
412 |
|
|
|
413 |
|
|
=end original |
414 |
|
|
|
415 |
|
|
各子プロセスはオプションとして1つのデータ構造を親プロセスに戻せます. |
416 |
|
|
データ構造とは,ここでは,文字列,ハッシュ,配列のリファレンスのことです. |
417 |
|
|
データ構造の内容はディスクに一時ファイルにL<Storable>モジュールのstore()メソッドで書き出されます. |
418 |
|
|
リファレンスはrun_on_finish コールバックに送ったコード内で取得されます. |
419 |
|
|
|
420 |
|
|
=begin original |
421 |
|
|
|
422 |
|
|
The data structure can be any scalar perl data structure which makes sense: |
423 |
|
|
string, numeric value or a reference to an array, hash or object. |
424 |
|
|
|
425 |
|
|
=end original |
426 |
|
|
|
427 |
|
|
データ構造は,次のようなどのようなスカラのデータ構造でも可能です: |
428 |
|
|
文字列,数値,配列やハッシュやオブジェクトのリファレンス |
429 |
|
|
|
430 |
|
|
=begin original |
431 |
|
|
|
432 |
|
|
There are 2 steps involved in retrieving data structures: |
433 |
|
|
|
434 |
|
|
=end original |
435 |
|
|
|
436 |
|
|
データ構造の取得に際して,2つのステップがあります. |
437 |
|
|
|
438 |
|
|
=begin original |
439 |
|
|
|
440 |
|
|
1) A reference to the data structure the child wishes to send back to the |
441 |
|
|
parent is provided as the second argument to the finish() call. It is up |
442 |
|
|
to the child to decide whether or not to send anything back to the parent. |
443 |
|
|
|
444 |
|
|
=end original |
445 |
|
|
|
446 |
|
|
1) 子プロセスが親プロセスに返したいデータ構造のリファレンスは |
447 |
|
|
finish()呼び出しの二番目の引数として与えられます. |
448 |
|
|
親に何かを返すかどうかは,子プロセス次第です. |
449 |
|
|
|
450 |
|
|
=begin original |
451 |
|
|
|
452 |
|
|
2) The data structure reference is retrieved using the callback provided in |
453 |
|
|
the run_on_finish() method. |
454 |
|
|
|
455 |
|
|
=end original |
456 |
|
|
|
457 |
|
|
2) データ構造のリファレンスはrun_on_finish メソッドで与えられたコールバックを使って |
458 |
|
|
取得されます. |
459 |
|
|
|
460 |
|
|
=begin original |
461 |
|
|
|
462 |
|
|
Keep in mind that data structure retrieval is not the same as returning a |
463 |
|
|
data structure from a method call. That is not what actually occurs. The |
464 |
|
|
data structure referenced in a given child process is serialized and |
465 |
|
|
written out to a file by L<Storable>. The file is subsequently read back |
466 |
|
|
into memory and a new data structure belonging to the parent process is |
467 |
|
|
created. Please consider the performance penality it can imply, so try to |
468 |
|
|
keep the returned structure small. |
469 |
|
|
|
470 |
|
|
=end original |
471 |
|
|
|
472 |
|
|
データ構造の取得はメソッド呼び出しからデータ構造が戻ってくるのとは,同じではないことを |
473 |
|
|
覚えておいてください.それは実際に起きているこではありません. |
474 |
|
|
子プロセスでリファレンスにされたデータ構造はシリアライズされ,L<Storable>でファイルに |
475 |
|
|
書き出されます.ファイルは順番にメモリに読み戻され,親プロセスに属する新しいデータ構造が |
476 |
|
|
作られます.これによるパフォーマンスの低下をよく考えて下さい.そして,戻す構造は小さく |
477 |
|
|
してください. |
478 |
|
|
|
479 |
|
|
=head1 例 |
480 |
|
|
|
481 |
|
|
=head2 並列 get |
482 |
|
|
|
483 |
|
|
=begin original |
484 |
|
|
|
485 |
|
|
This small example can be used to get URLs in parallel. |
486 |
|
|
|
487 |
|
|
=end original |
488 |
|
|
|
489 |
|
|
並列的にURLを取得する為に使用される簡単な例です. |
490 |
|
|
|
491 |
|
|
use Parallel::ForkManager; |
492 |
|
|
use LWP::Simple; |
493 |
|
|
my $pm=new Parallel::ForkManager(10); |
494 |
|
|
for my $link (@ARGV) { |
495 |
|
|
$pm->start and next; |
496 |
|
|
my ($fn)= $link =~ /^.*\/(.*?)$/; |
497 |
|
|
if (!$fn) { |
498 |
|
|
warn "Cannot determine filename from $fn\n"; |
499 |
|
|
} else { |
500 |
|
|
$0.=" ".$fn; |
501 |
|
|
print "Getting $fn from $link\n"; |
502 |
|
|
my $rc=getstore($link,$fn); |
503 |
|
|
print "$link downloaded. response code: $rc\n"; |
504 |
|
|
}; |
505 |
|
|
$pm->finish; |
506 |
|
|
}; |
507 |
|
|
|
508 |
|
|
=head2 コールバック |
509 |
|
|
|
510 |
|
|
=begin original |
511 |
|
|
|
512 |
|
|
Example of a program using callbacks to get child exit codes: |
513 |
|
|
|
514 |
|
|
=end original |
515 |
|
|
|
516 |
|
|
コールバックを使って,子プロセスがの終了コードを得る場合の例です: |
517 |
|
|
|
518 |
|
|
use strict; |
519 |
|
|
use Parallel::ForkManager; |
520 |
|
|
|
521 |
|
|
my $max_procs = 5; |
522 |
|
|
my @names = qw( Fred Jim Lily Steve Jessica Bob Dave Christine Rico Sara ); |
523 |
|
|
# hash to resolve PID's back to child specific information |
524 |
|
|
|
525 |
|
|
my $pm = new Parallel::ForkManager($max_procs); |
526 |
|
|
|
527 |
|
|
# Setup a callback for when a child finishes up so we can |
528 |
|
|
# get it's exit code |
529 |
|
|
# 子プロセスが終了する際に終了コードを取得する為の設定 |
530 |
|
|
$pm->run_on_finish( |
531 |
|
|
sub { my ($pid, $exit_code, $ident) = @_; |
532 |
|
|
print "** $ident just got out of the pool ". |
533 |
|
|
"with PID $pid and exit code: $exit_code\n"; |
534 |
|
|
} |
535 |
|
|
); |
536 |
|
|
|
537 |
|
|
$pm->run_on_start( |
538 |
|
|
sub { my ($pid,$ident)=@_; |
539 |
|
|
print "** $ident started, pid: $pid\n"; |
540 |
|
|
} |
541 |
|
|
); |
542 |
|
|
|
543 |
|
|
$pm->run_on_wait( |
544 |
|
|
sub { |
545 |
|
|
print "** Have to wait for one children ...\n" |
546 |
|
|
}, |
547 |
|
|
0.5 |
548 |
|
|
); |
549 |
|
|
|
550 |
|
|
foreach my $child ( 0 .. $#names ) { |
551 |
|
|
my $pid = $pm->start($names[$child]) and next; |
552 |
|
|
|
553 |
|
|
# This code is the child process |
554 |
|
|
# このコードは子プロセスです |
555 |
|
|
print "This is $names[$child], Child number $child\n"; |
556 |
|
|
sleep ( 2 * $child ); |
557 |
|
|
print "$names[$child], Child $child is about to get out...\n"; |
558 |
|
|
sleep 1; |
559 |
|
|
$pm->finish($child); # pass an exit code to finish |
560 |
|
|
} |
561 |
|
|
|
562 |
|
|
print "Waiting for Children...\n"; |
563 |
|
|
$pm->wait_all_children; |
564 |
|
|
print "Everybody is out of the pool!\n"; |
565 |
|
|
|
566 |
|
|
=head2 データ構造の取得 |
567 |
|
|
|
568 |
|
|
=begin original |
569 |
|
|
|
570 |
|
|
In this simple example, each child sends back a string reference. |
571 |
|
|
|
572 |
|
|
=end original |
573 |
|
|
|
574 |
|
|
この例では,各子プロセスは文字列のリファレンスを送っています. |
575 |
|
|
|
576 |
|
|
use Parallel::ForkManager 0.7.6; |
577 |
|
|
use strict; |
578 |
|
|
|
579 |
|
|
my $pm = new Parallel::ForkManager(2, '/server/path/to/temp/dir/'); |
580 |
|
|
|
581 |
|
|
# data structure retrieval and handling |
582 |
|
|
# データ構造の取得と取扱 |
583 |
|
|
$pm -> run_on_finish ( # called BEFORE the first call to start() |
584 |
|
|
sub { |
585 |
|
|
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_; |
586 |
|
|
|
587 |
|
|
# retrieve data structure from child |
588 |
|
|
# データ構造を子プロセスから取得 |
589 |
|
|
if (defined($data_structure_reference)) { # children are not forced to send anything |
590 |
|
|
my $string = ${$data_structure_reference}; # child passed a string reference |
591 |
|
|
print "$string\n"; |
592 |
|
|
} else { # problems occuring during storage or retrieval will throw a warning |
593 |
|
|
print qq|No message received from child process $pid!\n|; |
594 |
|
|
} |
595 |
|
|
} |
596 |
|
|
); |
597 |
|
|
|
598 |
|
|
# prep random statement components |
599 |
|
|
my @foods = ('chocolate', 'ice cream', 'peanut butter', 'pickles', 'pizza', 'bacon', 'pancakes', 'spaghetti', 'cookies'); |
600 |
|
|
my @preferences = ('loves', q|can't stand|, 'always wants more', 'will walk 100 miles for', 'only eats', 'would starve rather than eat'); |
601 |
|
|
|
602 |
|
|
# run the parallel processes |
603 |
|
|
# 並列プロセスを実行 |
604 |
|
|
my $person = ''; |
605 |
|
|
foreach $person (qw(Fred Wilma Ernie Bert Lucy Ethel Curly Moe Larry)) { |
606 |
|
|
$pm->start() and next; |
607 |
|
|
|
608 |
|
|
# generate a random statement about food preferences |
609 |
|
|
my $statement = $person . ' ' . $preferences[int(rand @preferences)] . ' ' . $foods[int(rand @foods)]; |
610 |
|
|
|
611 |
|
|
# send it back to the parent process |
612 |
|
|
# 親プロセスへ戻す |
613 |
|
|
$pm->finish(0, \$statement); # note that it's a scalar REFERENCE, not the scalar itself |
614 |
|
|
} |
615 |
|
|
$pm->wait_all_children; |
616 |
|
|
|
617 |
|
|
=begin original |
618 |
|
|
|
619 |
|
|
A second datastructure retrieval example demonstrates how children decide |
620 |
|
|
whether or not to send anything back, what to send and how the parent should |
621 |
|
|
process whatever is retrieved. |
622 |
|
|
|
623 |
|
|
=end original |
624 |
|
|
|
625 |
|
|
次のデータ構造を取得する例では,どのように子プロセスが何かを返すかどうかを決めているかと, |
626 |
|
|
どのように親プロセスが取得したものを処理するかをデモンストレーションしています. |
627 |
|
|
|
628 |
|
|
=for example begin |
629 |
|
|
|
630 |
|
|
use Parallel::ForkManager 0.7.6; |
631 |
|
|
use Data::Dumper; # to display the data structures retrieved. |
632 |
|
|
use strict; |
633 |
|
|
|
634 |
|
|
my $pm = new Parallel::ForkManager(20); # using the system temp dir $L<File::Spec>->tmpdir() |
635 |
|
|
|
636 |
|
|
# data structure retrieval and handling |
637 |
|
|
my %retrieved_responses = (); # for collecting responses |
638 |
|
|
$pm -> run_on_finish ( |
639 |
|
|
sub { |
640 |
|
|
my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_; |
641 |
|
|
|
642 |
|
|
# see what the child sent us, if anything |
643 |
|
|
if (defined($data_structure_reference)) { # test rather than assume child sent anything |
644 |
|
|
my $reftype = ref($data_structure_reference); |
645 |
|
|
print qq|ident "$ident" returned a "$reftype" reference.\n\n|; |
646 |
|
|
if (1) { # simple on/off switch to display the contents |
647 |
|
|
print &Dumper($data_structure_reference) . qq|end of "$ident" sent structure\n\n|; |
648 |
|
|
} |
649 |
|
|
|
650 |
|
|
# we can also collect retrieved data structures for processing after all children have exited |
651 |
|
|
$retrieved_responses{$ident} = $data_structure_reference; |
652 |
|
|
} else { |
653 |
|
|
print qq|ident "$ident" did not send anything.\n\n|; |
654 |
|
|
} |
655 |
|
|
} |
656 |
|
|
); |
657 |
|
|
|
658 |
|
|
# generate a list of instructions |
659 |
|
|
my @instructions = ( # a unique identifier and what the child process should send |
660 |
|
|
{'name' => '%ENV keys as a string', 'send' => 'keys'}, |
661 |
|
|
{'name' => 'Send Nothing'}, # not instructing the child to send anything back to the parent |
662 |
|
|
{'name' => 'Childs %ENV', 'send' => 'all'}, |
663 |
|
|
{'name' => 'Child chooses randomly', 'send' => 'random'}, |
664 |
|
|
{'name' => 'Invalid send instructions', 'send' => 'Na Na Nana Na'}, |
665 |
|
|
{'name' => 'ENV values in an array', 'send' => 'values'}, |
666 |
|
|
); |
667 |
|
|
|
668 |
|
|
my $instruction = ''; |
669 |
|
|
foreach $instruction (@instructions) { |
670 |
|
|
$pm->start($instruction->{'name'}) and next; # this time we are using an explicit, unique child process identifier |
671 |
|
|
|
672 |
|
|
# last step in child processing |
673 |
|
|
$pm->finish(0) unless $instruction->{'send'}; # no data structure is sent unless this child is told what to send. |
674 |
|
|
|
675 |
|
|
if ($instruction->{'send'} eq 'keys') { |
676 |
|
|
$pm->finish(0, \join(', ', keys %ENV)); |
677 |
|
|
|
678 |
|
|
} elsif ($instruction->{'send'} eq 'values') { |
679 |
|
|
$pm->finish(0, [values %ENV]); # kinda useless without knowing which keys they belong to... |
680 |
|
|
|
681 |
|
|
} elsif ($instruction->{'send'} eq 'all') { |
682 |
|
|
$pm->finish(0, \%ENV); # remember, we are not "returning" anything, just copying the hash to disc |
683 |
|
|
|
684 |
|
|
# demonstrate clearly that the child determines what type of reference to send |
685 |
|
|
} elsif ($instruction->{'send'} eq 'random') { |
686 |
|
|
my $string = q|I'm just a string.|; |
687 |
|
|
my @array = qw(I am an array); |
688 |
|
|
my %hash = (type => 'associative array', synonym => 'hash', cool => 'very :)'); |
689 |
|
|
my $return_choice = ('string', 'array', 'hash')[int(rand 3)]; # randomly choose return data type |
690 |
|
|
$pm->finish(0, \$string) if ($return_choice eq 'string'); |
691 |
|
|
$pm->finish(0, \@array) if ($return_choice eq 'array'); |
692 |
|
|
$pm->finish(0, \%hash) if ($return_choice eq 'hash'); |
693 |
|
|
|
694 |
|
|
# as a responsible child, inform parent that their instruction was invalid |
695 |
|
|
} else { |
696 |
|
|
$pm->finish(0, \qq|Invalid instructions: "$instruction->{'send'}".|); # ordinarily I wouldn't include invalid input in a response... |
697 |
|
|
} |
698 |
|
|
} |
699 |
|
|
$pm->wait_all_children; # blocks until all forked processes have exited |
700 |
|
|
|
701 |
|
|
# post fork processing of returned data structures |
702 |
|
|
for (sort keys %retrieved_responses) { |
703 |
|
|
print qq|Post processing "$_"...\n|; |
704 |
|
|
} |
705 |
|
|
|
706 |
|
|
=for example end |
707 |
|
|
|
708 |
|
|
=head1 バグ と 制限 |
709 |
|
|
|
710 |
|
|
=begin original |
711 |
|
|
|
712 |
|
|
Do not use Parallel::ForkManager in an environment, where other child |
713 |
|
|
processes can affect the run of the main program, so using this module |
714 |
|
|
is not recommended in an environment where fork() / wait() is already used. |
715 |
|
|
|
716 |
|
|
=end original |
717 |
|
|
|
718 |
|
|
Parallel::ForkManagerはこのような環境では使うことができません, |
719 |
|
|
別の子プロセスがメインプログラムに影響を与える場合, |
720 |
|
|
したがってこのモジュールを使用するのはfork() / wait()が既に使用される環境で推薦されません. |
721 |
|
|
|
722 |
|
|
=begin original |
723 |
|
|
|
724 |
|
|
If you want to use more than one copies of the Parallel::ForkManager, then |
725 |
|
|
you have to make sure that all children processes are terminated, before you |
726 |
|
|
use the second object in the main program. |
727 |
|
|
|
728 |
|
|
=end original |
729 |
|
|
|
730 |
|
|
一つ以上のParallel::ForkManagerを使用したい場合,主プログラムで第二のオブジェクトを使用する前に, |
731 |
|
|
全ての子プロセスが終了するのを確実にしなければなりません, |
732 |
|
|
|
733 |
|
|
=begin original |
734 |
|
|
|
735 |
|
|
You are free to use a new copy of Parallel::ForkManager in the child |
736 |
|
|
processes, although I don't think it makes sense. |
737 |
|
|
|
738 |
|
|
=end original |
739 |
|
|
|
740 |
|
|
あなたは子プロセスに自由にParallel::ForkManagerの新しいコピーを使用することができます, |
741 |
|
|
私はそれに意味があるとは思いませんが. |
742 |
|
|
|
743 |
|
|
=head1 COPYRIGHT |
744 |
|
|
|
745 |
|
|
Copyright (c) 2000-2010 Szabó, Balázs (dLux) |
746 |
|
|
|
747 |
|
|
All right reserved. This program is free software; you can redistribute it |
748 |
|
|
and/or modify it under the same terms as Perl itself. |
749 |
|
|
|
750 |
|
|
=head1 作者 |
751 |
|
|
|
752 |
|
|
dLux (Szabó, Balázs) <dlux@kapu.hu> |
753 |
|
|
|
754 |
|
|
=head1 クレジット |
755 |
|
|
|
756 |
|
|
Noah Robin <sitz@onastick.net> (documentation tweaks) |
757 |
|
|
Chuck Hirstius <chirstius@megapathdsl.net> (callback exit status, example) |
758 |
|
|
Grant Hopwood <hopwoodg@valero.com> (win32 port) |
759 |
|
|
Mark Southern <mark_southern@merck.com> (bugfix) |
760 |
|
|
Ken Clarke <www.perlprogrammer.net> (datastructure retrieval) |
761 |
|
|
|
762 |
|
|
=head1 翻訳者 |
763 |
|
|
|
764 |
|
|
atsushi kobayashi(nekokak@users.sourceforge.jp) |
765 |
|
|
Atsushi Kato (ktat@cpan.org) -- 0.75 から 0.79 の差分を翻訳 |