Parall...Prefork(3pm)
π Parallel::Prefork π
okay so bestie, pull up a chair, we're talking about Parallel::Prefork β a simple prefork server framework, and yes it's from the Perl user-contributed docs, the Parall...Prefork(3pm) one, you found it, slay. π
It's a lil module that lives at Parallel::Prefork and it's about to make your forking life so much cuter.
β¨ SYNOPSIS β¨
here's the whole vibe in one go, look at her:
use Parallel::Prefork;
my $pm = Parallel::Prefork->new({
max_workers => 10,
trap_signals => {
TERM => 'TERM',
HUP => 'TERM',
USR1 => undef,
}
});
while ($pm->signal_received ne 'TERM') {
load_config();
$pm->start(sub {
... do some work within the child process ...
});
}
$pm->wait_all_children();
that's it. that's the whole serve. π
πΈ DESCRIPTION πΈ
okay so Parallel::Prefork is much like Parallel::ForkManager, but β and this is the part that matters β it supports graceful shutdown and run-time reconfiguration. it's giving glow-up. it's giving main character energy. no cap. π₯
π¦ METHODS π¦
π new
instantiation, babes. she takes a hashref as an argument. the recognized attributes are as follows:
max_workersβ number of worker processes (default:10). that's your lil army. πspawn_intervalβ interval in seconds between spawning child processes unless a child process exits abnormally (default:0). so like, chill spacing between spawns, unless something goes wrong, then all bets are off.err_respawn_intervalβ number of seconds to deter spawning of child processes after a worker exits abnormally (default:1). a lil cooldown. a lil breather. we do not spam-fork when things are messy. πtrap_signalsβ hashref of signals to be trapped. the manager process will trap the signals listed in the keys of the hash, and send the signal specified in the associated value (if any) to all worker processes.
and here's the nuance, don't skip it:
- if the associated value is a scalar, then it is treated as the name of the signal to be sent immediately to all the worker processes.
- if the value is an arrayref, the first value is treated as the name of the signal and the second value is treated as the interval (in seconds) between sending the signal to each worker process.
so scalar = yeet it at everyone right now. arrayref = send it, but pace yourself. π§
on_child_reapβ coderef that is called when a child is reaped. receives the instance to the currentParallel::Prefork, the child's pid, and its exit status. three lil gifts, one callback. π«Άbefore_forkafter_fork
coderefs that are called in the manager process before and after fork, if being set. (they're listed like that in the original and we respect it β two separate attributes, both optional, both only fire in the manager.) β¨
π start
the main routine, bestie. there are two ways to use the function.
- if given a subref as an argument, it forks child processes and executes that subref within the child processes. the processes will exit with
0status when the subref returns. clean, simple, gorgeous. β - the other way is to not give any arguments to the function. the function returns
undefin child processes. the caller should execute the application logic and then callfinishto terminate the process. you're driving now. π
the start function returns true within the manager process upon receiving a signal specified in the trap_signals hashref.
π finish
child processes (when executed by a zero-argument call to start) should call this function for termination. takes exit code as an optional argument. only usable from child processes. managers, hands off, this one isn't yours. π
π signal_all_children
sends signal to all worker processes. only usable from manager process. it's the manager's lil megaphone. π£
β wait_all_children()
β wait_all_children($timeout)
waits until all worker processes exit or timeout (given as an optional argument in seconds) exceeds. the method returns the number of the worker processes still running.
so if she hands you back a 0, everyone clocked out. if she hands you a number, that's how many are still hanging around. π
π AUTHOR π
Kazuho Oku
π LICENSE π
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
see http://www.perl.com/perl/misc/Artistic.html
okay that's the whole page, no crumbs left. every flag, every default, every callback, every exit status β all accounted for. π―
and that's a wrap, bestie β this is from perl v5.34.0, dated 2022-05-24, and it's the Parall...Prefork(3pm) page. go forth and fork responsibly. ππ
π¦
π¦ where this page comes from
- debian bookworm: libparallel-prefork-perl 0.18-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libparallel-prefork-perl 0.18-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libparallel-prefork-perl 0.18-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libparallel-prefork-perl 0.18-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libparallel-prefork-perl 0.18-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms