Acme-Fork-Lazy

 view release on metacpan or  search on metacpan

inc/Module/Install/Win32.pm  view on Meta::CPAN


  http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe
      or
  ftp://ftp.microsoft.com/Softlib/MSLFILES/Nmake15.exe

Please download the file manually, save it to a directory in %PATH% (e.g.
C:\WINDOWS\COMMAND\), then launch the MS-DOS command line shell, "cd" to
that directory, and run "Nmake15.exe" from there; that will create the
'nmake.exe' file needed by this module.

You may then resume the installation process described in README.

-------------------------------------------------------------------------------
END_MESSAGE

}

1;

lib/Acme/Fork/Lazy.pm  view on Meta::CPAN

 say $_ for @list;

 ###

 END {
    wait_kids; # make sure we're not leaving behind any zombies
 }

=head1 DESCRIPTION

We often want to fork a process with an expensive calculation.  This involves making the child
write the answer back to the parent, who will then have to poll the child occasionally to check
if it answered back.  There are abstractions, like L<Poe::Wheel::Run> (lovely if you're already
using L<POE>).  This is another one, using lazy variables:

=head2 C<forked>

 my $foo = forked { do_calculation() };
 print "The answer was $foo\n";

C<forked> returns a lazy calculation that will wait on the child process and return its
result as a Perl data structure.  If the child process isn't ready, then it will wait for it.
This means that you could just as easily do:

 my $foo = forked { do_calculation() };
 do_some_stuff_that_might_take_about_the_same_time_as_calculation();
 print "The answer was $foo\n";

without having to worry about polling etc. if the work in the main process didn't quite take
long enough.

Note that the forked result must be a scalar.  

=head2 C<wait_kids>

 END {
     wait_kids();
 }

lib/Acme/Fork/Lazy.pm  view on Meta::CPAN

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

t/01_basic.t  view on Meta::CPAN

use Test::More;
use Acme::Fork::Lazy ':all';

# SCALAR CALC
my $foo = forked { 2+3 };
is ($foo, 5, 'Simple deferred calculation');

PARALLEL: {
    my @list = map forked { sleep $_; $_*3 }, 1..4;

    sleep 2; # gives time for half of list to be processed
    my $t = time;
    is_deeply( \@list, [3,6,9,12], 'List was correct' );
    my $delta = time-$t;
    # we'd expect to have been at least another 2 seconds
    ok( ($delta >= 2) && ($delta <= 3), 'Waited ca another 2 seconds to process rest of list');
}

# COMPLEX CALC
my $complex = forked { [1,2] };
TODO: {
    local $TODO = 1;
    is_deeply( $complex, [1,2], "Complex value is forced correctly" );
}
is_deeply( [@$complex], [1,2], "Complex value is correct, if manually forced" );



( run in 0.321 second using v1.01-cache-2.11-cpan-8d75d55dd25 )