The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Parallel::WorkUnit - Provide easy-to-use forking with ability to pass back data

VERSION

version 1.001

SYNOPSIS

  my $wu = Parallel::WorkUnit->new();
  $wu->async( sub { ... }, \&callback );

  $wu->waitall();

DESCRIPTION

This is a very simple forking implementation of parallelism, with the ability to pass data back from the asyncronous child process in a relatively efficient way (with the limitation of using a pipe to pass the information, serialized, back). It was designed to be very simple for a developer to use, with the ability to pass reasonably large amounts of data back to the parent process.

There are many other Parallel::* applications in CPAN - it would be worth any developer's time to look through those and choose the best one.

METHODS

new

Create a new workunit class.

async( sub { ... }, \&callback )

Spawns work on a new forked process. The forked process inherits all Perl state from the parent process, as would be expected with a standard fork() call. The child shares nothing with the parent, other than the return value of the work done.

The work is specified either as a subroutine reference or an anonymous sub (sub { ... }) and should return a scalar. Any scalar that Storable's freeze() method can deal with is acceptable (for instance, a hash reference or undef).

When the work is completed, it serializes the result and streams it back to the parent process via a pipe. The parent, in a waitall() call, will call the callback function with the unserialized return value.

Should the child process die, the parent process will also die (inside the waitall() method).

The PID of the child is returned to the parent process when this method is executed.

waitall()

Called from the parent method while waiting for the children to exit. This method handles children that die() or return a serializable data structure. When all children return, this method will return.

If a child dies unexpectedly, this method will die() and propagate a modified exception.

This method has a side-effect of reaping any children that haven't yet been reaped. If you aren't messing with $SIG{CHLD}, you do not need to worry about this.

wait($pid)

This functions simiarly to waitall(), but only waits for a single PID. See the waitall() documentation above for details.

If wait() is called on a process that is already done executing, it simply returns. Otherwise, it waits until the child process's work unit is complete and executes the callback routine, then returns.

This method has a side-effect of reaping any children that haven't yet been reaped. If you aren't messing with $SIG{CHLD}, you do not need to worry about this.

SIDE EFFECTS

This module, when instantiated, sets the SIG{CHLD} handler to 'IGNORE'. This means that children will be automatically reaped on most systems. In addition, the wait() and waitall() methods will attempt to reap any outstanding children.

AUTHOR

Joel Maslak <jmaslak@antelope.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Joel Maslak.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.