NAME

Doit::Fork - run code in another process

SYNOPSIS

use Doit;
use Doit::Log;
sub run_test {
    my(undef, $arg) = @_;
    return "running in process $$ with arg $arg";
}
return 1 if caller;
my $doit = Doit->init;
$doit->add_component('fork');
my $fork = $doit->do_fork;
info $fork->call_with_runner('run_test', 'test argument');
undef $fork;
info $Doit::Fork::last_exits[-1]->{msg};

DESCRIPTION

This component provides a function do_fork, which forks a new process and returns a new Doit runner (like "do_ssh_connect" in Doit or "do_sudo" in Doit. This process is capable of running subroutines (using call_with_runner or call), or any other core Doit function.

A possible use case is to have multiple separated processes, but with the ability (unlike with normal forked processes) to call subroutines in these processes with possibly complex arguments and return values.

Note that all communication is still synchronous.

MODULE VARIABLES

@last_exits

After destroying a fork (e.g. by using undef $fork or on scope exits) the worker will exit, the process will be reaped and the exit information will be put into the module variable @last_exits as a hash with at least the following elements: exitcode, pid, msg. Only the information of the last 10 exits will be kept. This number can be changed by setting $keep_last_exits.

$keep_last_exits

The number of process information kept. By default 10.

AUTHOR

Slaven Rezic <srezic@cpan.org>

COPYRIGHT

Copyright (c) 2017,2023 Slaven Rezic. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Doit, "fork" in perlfunc