Dmitry Karasik > IO-Lambda > IO::Lambda::Fork

Download:
IO-Lambda-1.23.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Source  

NAME ^

IO::Lambda::Fork - wait for blocking code in children processes

DESCRIPTION ^

The module implements the lambda wrapper that allows to wait asynchronously for blocking code in another process' context. IO::Lambda::Fork provides a twofold interface for that: the lambda interface, that can wait for the forked child processes, and an easier way for simple communication between these.

Contrary to the classical stdin-stdout interaction between parent and child processes, this module establishes a stream socket and uses it instead. The socket can also be used by the caller for its own needs ( see IO::Lambda::Message ).

SYNOPSIS ^

    use IO::Lambda qw(:lambda);
    use IO::Lambda::Fork qw(forked);

Blocking wait

    lambda {
        context forked {
            sleep(1);
            return "hello!";
        };
        tail {
            print shift, "\n"
        }
    }-> wait;

    # hello!

Non-blocking wait

    lambda {
        context 0.1, forked {
              sleep(1);
              return "hello!";
        };
        any_tail {
            if ( @_) {
                print "done: ", $_[0]-> peek, "\n";
            } else {
                print "not yet\n";
                again;
            }
        };
    }-> wait;

    # not yet
    # not yet
    # not yet
    # done: hello!

(of course, since IO::Lambda is inherently non-blocking, the first example is of much more use, as many of such "blocking" lambdas can execute in parallel)

API ^

new_process($code, $pass_socket, @param) -> ( $pid, $socket | undef, $error )

Forks a process, and sets up a read-write socket between the parent and the child. On success, returns the child's pid and the socket, where the latter is passed further to $code. On failure, returns undef and $!.

This function does not create a lambda, neither makes any preparations for waiting for the child process, nor for reaping its status. It is therefore important for the caller itself to wait for the child process, to avoid zombie processes. That can be done either synchronously:

    my ( $pid, $reader) = new_process {
        my $writer = shift;
        print $writer, "Hello world!\n";
    };
    print while <$reader>;
    close($reader);
    waitpid($pid, 0);

or asynchronously, using waitpid wrappers from IO::Lambda::Socket:

    use IO::Lambda::Signal qw(pid new_pid);
    ...
    lambda { context $pid; &pid() }-> wait;
    # or
    new_pid($pid)-> wait;
process($code) :: () -> ($? | undef)

Creates a simple lambda that forks a process and executes $code inside it. The lambda returns the child exit code.

new_forked($code) :: () -> ( $?, ( 1, @results | undef, $error))

Creates a lambda that awaits for $code in a sub-process to be executed, then returns the code' result back to the parent. Returns also the process exitcode, $code eval success flag, and an array of code results or an error string, if any.

forked($code) :: () -> (@results | $error)

A simple wrapper over new_forked, that returns either $code results or an error string.

BUGS ^

Doesn't work on Win32, because relies on $SIG{CHLD} which is not getting delivered (on 5.10.0 at least). However, since Win32 doesn't have forks anyway, Perl emulates them with threads. Consider using IO::Lambda::Thread instead when running on windows.

Has issues with SIGCHLD on perls < 5.8.0.

AUTHOR ^

Dmitry Karasik, <dmitry@karasik.eu.org>.

syntax highlighting: