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 NOTE: Parallel::Pipes provides low-level interfaces. If you are
49 interested in using Parallel::Pipes, you may want to look at
50 Parallel::Pipes::App instead, which provides more friendly interfaces.
51
52 Parallel processing is essential, but it is also difficult:
53
54 How can we synchronize our workers?
55 More precisely, how to detect our workers are ready or finished.
56
57 How can we communicate with our workers?
58 More precisely, how to collect results of tasks.
59
60 Parallel::Pipes tries to solve these problems with pipe(2) and
61 select(2).
62
63 App::cpm, a fast CPAN module installer, uses Parallel::Pipes. Please
64 look at App::cpm
65 <https://github.com/skaji/cpm/blob/master/lib/App/cpm/CLI.pm> or eg
66 directory <https://github.com/skaji/Parallel-Pipes/tree/main/eg> for
67 real world usages.
68
70 Shoichi Kaji <skaji@cpan.org>
71
73 Copyright 2016 Shoichi Kaji <skaji@cpan.org>
74
75 This library is free software; you can redistribute it and/or modify it
76 under the same terms as Perl itself.
77
78
79
80perl v5.36.0 2022-07-22 Parallel::Pipes(3pm)