Acme-Fork-Lazy
view release on metacpan or search on metacpan
lib/Acme/Fork/Lazy.pm view on Meta::CPAN
=item L<Scalar::Defer>
The original, by Audrey Tang
=item L<Data::Thunk>
An ambitious and complex implementation by Nothingmuch.
=item L<Scalar::Lazy>
A much simpler implementation.
=back
=item *
The result is currently sent back from the child process coded in L<YAML>.
=item *
If you can stomach POE, look at L<POE::Wheel::Run>
=item *
Various IPC modules wrap C<fork> in more or less palatable ways: L<IPC::Run>, L<Proc::Fork>, etc.
=back
=head1 AUTHOR and LICENSE
(C) 2008 osfameron@cpan.org
This module is distributed under the same conditions as Perl itself.
=cut
our $VERSION = 0.03;
use IO::Pipe;
use YAML;
# use Data::Thunk;
# use Scalar::Lazy;
use Scalar::Defer;
# all need a kludge for reference example
use base 'Exporter';
our %EXPORT_TAGS = (
all => [ qw/ forked wait_kids / ],
);
Exporter::export_ok_tags('all');
sub forked (&) {
my $sub = shift;
my $p = IO::Pipe->new();
if (my $child = fork) {
$p->reader;
return lazy {
waitpid $child, 0;
local $/ = undef;
my $result = <$p>;
Load($result);
};
} else {
$p->writer;
my @result = $sub->();
print $p Dump(@result);
exit;
}
}
sub wait_kids {
# Wait on all kids, possibly getting rid of zombies etc.
use POSIX ":sys_wait_h";
my $kid;
do {
$kid = waitpid(-1, 0);
} while $kid > 0;
}
1;
( run in 2.435 seconds using v1.01-cache-2.11-cpan-d80b1682f3f )