IO::Lambda::Fork(3)

πŸ“¦ version 1.34, in alpine 3.24

🌸 NAME 🌸

IO::Lambda::Fork(3) β€” this is the page for IO::Lambda::Fork, and its whole iconic thing is: wait for blocking code in children processes. πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

This module implements the lambda wrapper that lets you wait asynchronously for blocking code in another process' context. IO::Lambda::Fork gives you a twofold interface for that: the lambda interface that can wait for forked child processes, and an easier way for simple communication between them.

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). Lowkey iconic. ✨

✨ SYNOPSIS ✨

Okay bestie, here's the setup:

    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!

And 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. No cap. 🫢

πŸ‘‘ 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 $!.
Careful, bestie: this function does not create a lambda, and it does not make any preparations for waiting for the child process or 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's 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 it 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>.


okay bye bestie β€” this page is perl v5.42.2, dated 2026-02-15, and it's IO::Lambda::Fork(3) πŸ’…πŸ«Άβœ¨

πŸ“¦ where this page comes from