1Parallel::Pipes(3pm) User Contributed Perl Documentation Parallel::Pipes(3pm)
2
3
4
6 Parallel::Pipes - parallel processing using pipe(2) for communication
7 and synchronization
8
10 use Parallel::Pipes;
11
12 my $pipes = Parallel::Pipes->new(5, sub {
13 # this is a worker code
14 my $task = shift;
15 my $result = do_work($task);
16 return $result;
17 });
18
19 my $queue = Your::TaskQueue->new;
20 # wrap Your::TaskQueue->get
21 my $get; $get = sub {
22 my $queue = shift;
23 if (my @task = $queue->get) {
24 return @task;
25 }
26 if (my @written = $pipes->is_written) {
27 my @ready = $pipes->is_ready(@written);
28 $queue->register($_->read) for @ready;
29 return $queue->$get;
30 } else {
31 return;
32 }
33 };
34
35 while (my @task = $queue->$get) {
36 my @ready = $pipes->is_ready;
37 $queue->register($_->read) for grep $_->is_written, @ready;
38 my $min = List::Util::min($#task, $#ready);
39 for my $i (0..$min) {
40 # write tasks to pipes which are ready
41 $ready[$i]->write($task[$i]);
42 }
43 }
44
45 $pipes->close;
46
48 THIS IS EXPERIMENTAL.
49
50 Parallel processing is essential, but it is also difficult:
51
52 How can we synchronize our workers?
53 More precisely, how to detect our workers are ready or finished.
54
55 How can we communicate with our workers?
56 More precisely, how to collect results of tasks.
57
58 Parallel::Pipes tries to solve these problems with pipe(2) and
59 select(2).
60
61 App::cpm, a fast CPAN module installer, uses Parallel::Pipes. Please
62 look at App::cpm
63 <https://github.com/skaji/cpm/blob/master/lib/App/cpm.pm> or eg
64 directory <https://github.com/skaji/Parallel-Pipes/tree/master/eg> for
65 real world usages.
66
68 new
69 my $pipes = Parallel::Pipes->new($number, $code);
70
71 The constructor, which takes
72
73 number
74 The number of workers.
75
76 code
77 Worker's code.
78
79 is_ready
80 my @ready = $pipes->is_ready;
81 my @ready = $pipes->is_ready(@pipes);
82
83 Get pipes which are ready to write.
84
85 is_written
86 my @written = $pipes->is_written;
87
88 Get pipes which are written.
89
90 close
91 $pipes->close;
92
93 Close pipes (also shutdown workers).
94
96 Shoichi Kaji <skaji@cpan.org>
97
99 Copyright 2016 Shoichi Kaji <skaji@cpan.org>
100
101 This library is free software; you can redistribute it and/or modify it
102 under the same terms as Perl itself.
103
104
105
106perl v5.32.0 2020-07-28 Parallel::Pipes(3pm)