| 1 | #!/usr/bin/perl | ||||||||||
| 2 | 7 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Acme::Fork::Lazy; | ||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Acme::Fork::Lazy - abstract forking with lazy variables | ||||||||||
| 9 | |||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||
| 11 | |||||||||||
| 12 | use Acme::Fork::Lazy qw/:all/; | ||||||||||
| 13 | use feature 'say'; | ||||||||||
| 14 | |||||||||||
| 15 | ### | ||||||||||
| 16 | # Single parallel calculation | ||||||||||
| 17 | |||||||||||
| 18 | my $foo = forked { expensive_calculation_to_do_in_parallel() }; | ||||||||||
| 19 | # ...then (sooner or later...) | ||||||||||
| 20 | say $foo; | ||||||||||
| 21 | |||||||||||
| 22 | ### | ||||||||||
| 23 | # Parallel map | ||||||||||
| 24 | |||||||||||
| 25 | my @list = map forked { sleep $_; $_*2 }, 1..10; | ||||||||||
| 26 | sleep 5; # gives enough time for first 5 elements to be calculated | ||||||||||
| 27 | say $_ for @list; | ||||||||||
| 28 | |||||||||||
| 29 | ### | ||||||||||
| 30 | |||||||||||
| 31 | END { | ||||||||||
| 32 | wait_kids; # make sure we're not leaving behind any zombies | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||
| 36 | |||||||||||
| 37 | We often want to fork a process with an expensive calculation. This involves making the child | ||||||||||
| 38 | write the answer back to the parent, who will then have to poll the child occasionally to check | ||||||||||
| 39 | if it answered back. There are abstractions, like L<Poe::Wheel::Run> (lovely if you're already | ||||||||||
| 40 | using L<POE>). This is another one, using lazy variables: | ||||||||||
| 41 | |||||||||||
| 42 | =head2 C<forked> | ||||||||||
| 43 | |||||||||||
| 44 | my $foo = forked { do_calculation() }; | ||||||||||
| 45 | print "The answer was $foo\n"; | ||||||||||
| 46 | |||||||||||
| 47 | C<forked> returns a lazy calculation that will wait on the child process and return its | ||||||||||
| 48 | result as a Perl data structure. If the child process isn't ready, then it will wait for it. | ||||||||||
| 49 | This means that you could just as easily do: | ||||||||||
| 50 | |||||||||||
| 51 | my $foo = forked { do_calculation() }; | ||||||||||
| 52 | do_some_stuff_that_might_take_about_the_same_time_as_calculation(); | ||||||||||
| 53 | print "The answer was $foo\n"; | ||||||||||
| 54 | |||||||||||
| 55 | without having to worry about polling etc. if the work in the main process didn't quite take | ||||||||||
| 56 | long enough. | ||||||||||
| 57 | |||||||||||
| 58 | Note that the forked result must be a scalar. | ||||||||||
| 59 | |||||||||||
| 60 | =head2 C<wait_kids> | ||||||||||
| 61 | |||||||||||
| 62 | END { | ||||||||||
| 63 | wait_kids(); | ||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | Place this anywhere that you'd like to stop and wait for the children to catch up, | ||||||||||
| 67 | and in particular in an END block to avoid producing zombies. | ||||||||||
| 68 | |||||||||||
| 69 | =head1 BUGS and TODO | ||||||||||
| 70 | |||||||||||
| 71 | Lots. Once those are resolved, we could upgrade this from C<Acme::> to C<Proc::Forked::Lazy>. | ||||||||||
| 72 | |||||||||||
| 73 | =over 4 | ||||||||||
| 74 | |||||||||||
| 75 | =item * | ||||||||||
| 76 | |||||||||||
| 77 | The Lazy modules (see L</SEE ALSO>) are all currently broken in various | ||||||||||
| 78 | exciting ways. So you may find that certain uses (like using L<Data::Dumper> | ||||||||||
| 79 | to output the result) won't trigger forcing the lazy result, and so on. | ||||||||||
| 80 | |||||||||||
| 81 | =item * | ||||||||||
| 82 | |||||||||||
| 83 | Only scalar values may be returned by a C<forked> block. | ||||||||||
| 84 | |||||||||||
| 85 | =item * | ||||||||||
| 86 | |||||||||||
| 87 | No attempt is made to handle failure: timeouts/retry/error etc. | ||||||||||
| 88 | |||||||||||
| 89 | =item * | ||||||||||
| 90 | |||||||||||
| 91 | The client has to manually call C<wait_kids> in END to make sure all kids | ||||||||||
| 92 | exited cleanly. | ||||||||||
| 93 | |||||||||||
| 94 | =back | ||||||||||
| 95 | |||||||||||
| 96 | Suggestions and patches for any of the above are very welcome (as well as new | ||||||||||
| 97 | bug reports!) | ||||||||||
| 98 | |||||||||||
| 99 | =head1 SEE ALSO | ||||||||||
| 100 | |||||||||||
| 101 | =over 4 | ||||||||||
| 102 | |||||||||||
| 103 | =item * | ||||||||||
| 104 | |||||||||||
| 105 | The lazy semantics are provided by one of the following: | ||||||||||
| 106 | |||||||||||
| 107 | =over 8 | ||||||||||
| 108 | |||||||||||
| 109 | =item L<Scalar::Defer> | ||||||||||
| 110 | |||||||||||
| 111 | The original, by Audrey Tang | ||||||||||
| 112 | |||||||||||
| 113 | =item L<Data::Thunk> | ||||||||||
| 114 | |||||||||||
| 115 | An ambitious and complex implementation by Nothingmuch. | ||||||||||
| 116 | |||||||||||
| 117 | =item L<Scalar::Lazy> | ||||||||||
| 118 | |||||||||||
| 119 | A much simpler implementation. | ||||||||||
| 120 | |||||||||||
| 121 | =back | ||||||||||
| 122 | |||||||||||
| 123 | =item * | ||||||||||
| 124 | |||||||||||
| 125 | The result is currently sent back from the child process coded in L<YAML>. | ||||||||||
| 126 | |||||||||||
| 127 | =item * | ||||||||||
| 128 | |||||||||||
| 129 | If you can stomach POE, look at L<POE::Wheel::Run> | ||||||||||
| 130 | |||||||||||
| 131 | =item * | ||||||||||
| 132 | |||||||||||
| 133 | Various IPC modules wrap C<fork> in more or less palatable ways: L<IPC::Run>, L<Proc::Fork>, etc. | ||||||||||
| 134 | |||||||||||
| 135 | =back | ||||||||||
| 136 | |||||||||||
| 137 | =head1 AUTHOR and LICENSE | ||||||||||
| 138 | |||||||||||
| 139 | (C) 2008 osfameron@cpan.org | ||||||||||
| 140 | |||||||||||
| 141 | This module is distributed under the same conditions as Perl itself. | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||
| 145 | 7 | our $VERSION = 0.03; | |||||||||
| 146 | |||||||||||
| 147 | 7 | ▶ | use IO::Pipe; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 148 | 7 | ▶ | use YAML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 149 | # use Data::Thunk; | ||||||||||
| 150 | # use Scalar::Lazy; | ||||||||||
| 151 | 7 | ▶ | use Scalar::Defer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 152 | # all need a kludge for reference example | ||||||||||
| 153 | |||||||||||
| 154 | 7 | ▶ | use base 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 155 | 7 | our %EXPORT_TAGS = ( | |||||||||
| 156 | all => [ qw/ forked wait_kids / ], | ||||||||||
| 157 | ); | ||||||||||
| 158 | 7 | Exporter::export_ok_tags('all'); | |||||||||
| 159 | |||||||||||
| 160 | 27 | ▶ | sub forked (&) { | ||||||||
|
Subroutine
forked: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 161 | 27 | my $sub = shift; | |||||||||
| 162 | 27 | my $p = IO::Pipe->new(); | |||||||||
| 163 | |||||||||||
| 164 | 27 | ▶ | if (my $child = fork) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 21 | $p->reader; | |||||||||
| 166 | 15 | ▶ | return lazy { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 167 | 15 | waitpid $child, 0; | |||||||||
| 168 | 15 | local $/ = undef; | |||||||||
| 169 | 15 | my $result = <$p>; | |||||||||
| 170 | 15 | Load($result); | |||||||||
| 171 | 21 | }; | |||||||||
| 172 | } else { | ||||||||||
| 173 | 6 | $p->writer; | |||||||||
| 174 | 6 | my @result = $sub->(); | |||||||||
| 175 | 6 | print $p Dump(@result); | |||||||||
| 176 | 6 | exit; | |||||||||
| 177 | } | ||||||||||
| 178 | } | ||||||||||
| 179 | 1 | ▶ | sub wait_kids { | ||||||||
|
Subroutine
wait_kids: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 180 | # Wait on all kids, possibly getting rid of zombies etc. | ||||||||||
| 181 | 7 | ▶ | use POSIX ":sys_wait_h"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 182 | 1 | my $kid; | |||||||||
| 183 | 1 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | 1 | $kid = waitpid(-1, 0); | |||||||||
| 185 | } while $kid > 0; | ||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 7 | 1; | |||||||||