AnyEve...Manager(3pm)
π NAME π
AnyEvent::ForkManager β a super cute, super simple parallel processing fork manager that runs on AnyEvent, bestie. It's giving multitasking but make it non-blocking. π¦
β¨ VERSION β¨
This doc is describing AnyEvent::ForkManager version 0.07. Yes, she's still a baby but she's an icon. π
π SYNOPSIS π
okay so here's the whole vibe, copy-paste ready:
use AnyEvent;
use AnyEvent::ForkManager;
use List::Util qw/shuffle/;
my $MAX_WORKERS = 10;
my $pm = AnyEvent::ForkManager->new(max_workers => $MAX_WORKERS);
$pm->on_start(sub {
my($pm, $pid, $sec) = @_;
printf "start sleep %2d sec.\n", $sec;
});
$pm->on_finish(sub {
my($pm, $pid, $status, $sec) = @_;
printf "end sleep %2d sec.\n", $sec;
});
my @sleep_time = shuffle(1 .. 20);
foreach my $sec (@sleep_time) {
$pm->start(
cb => sub {
my($pm, $sec) = @_;
sleep $sec;
},
args => [$sec]
);
}
my $cv = AnyEvent->condvar;
# wait with non-blocking
$pm->wait_all_children(
cb => sub {
my($pm) = @_;
print "end task!\n";
$cv->send;
},
);
$cv->recv;
like, twenty sleeps, ten workers, zero blocking. main character energy fr fr. π
πΈ DESCRIPTION πΈ
AnyEvent::ForkManager is basically Parallel::ForkManager's cooler cousin β same energy, but she supports a non-blocking interface with AnyEvent. β¨
Here's the tea: Parallel::ForkManager is useful, but honestly it's a struggle to use alongside AnyEvent. Why? because some of Parallel::ForkManager's methods are blocking the event loop of the AnyEvent. π Not the move.
So you can accomplish the same goals without adversely affecting the Parallel::ForkManager to AnyEvent::ForkManager with AnyEvent. And the reason that works so well? Because AnyEvent::ForkManager's methods are non-blocking the event loop of the AnyEvent. no cap. π«Ά
π§ INTERFACE π§
Methods
π new
This is constructor. the origin story. the birth. π£
max_workersβ max parallel forking count. (default:10)on_startβ started child process callback.on_finishβ finished child process callback.on_errorβ fork error callback.on_enqueueβ If push to start up child process queue, this callback is called.on_dequeueβ If shift from start up child process queue, this callback is called.on_working_maxβ If request to start up child process and process count equal max process count, this callback is called.
Example
my $pm = AnyEvent::ForkManager->new(
max_workers => 2, ## default 10
on_finish => sub { ## optional
my($pid, $status, @anyargs) = @_;
## this callback call when finished child process.(like AnyEvent->child)
},
on_error => sub { ## optional
my($pm, @anyargs) = @_;
## this callback call when fork failed.
},
);
π¦ start
start child process. go bestie go πβοΈβ¨
argsβ arguments passed to the callback function of the child process.cbβ run on child process callback.
Example
$pm->start(
cb => sub { ## optional
my($pm, $job_id) = @_;
## this callback call in child process.
},
args => [$job_id],## this arguments passed to the callback function
);
β wait_all_children
You can call this method to wait for all the processes which have been forked. She can wait with blocking or wait with non-blocking in event loop of AnyEvent. π«
β οΈ heads up bestie: the feature to wait with blocking is ALPHA quality till the version hits v1.0.0. Things might be broken. like, genuinely. not being dramatic. π
blockingβ If this parameter is true, blocking wait enable. (default:false) feature to wait with blocking is ALPHA quality till the version hitsv1.0.0. Things might be broken.cbβ finished all the processes callback.
Example
$pm->wait_all_children(
cb => sub { ## optional
my($pm) = @_;
## this callback call when finished all child process.
},
);
π signal_all_children
Sends signal to all worker processes. Only usable from manager process. π£ everyone hear me out, simultaneously.
π on_error
As a new method's argument.
π on_start
As a new method's argument.
πΈ on_finish
As a new method's argument.
π on_enqueue
As a new method's argument.
π§ on_dequeue
As a new method's argument.
π on_working_max
As a new method's argument.
π₯ DEPENDENCIES π₯
Perl 5.8.1 or later. old school but she ate. π
π BUGS π
All complex software has bugs lurking in it, and this module is no exception. π If you find a bug please either email me, or add the bug to cpan-RT. do NOT suffer in silence bestie, tell me.
π«Ά SEE ALSO π«Ά
AnyEvent AnyEvent::Util Parallel::ForkManager Parallel::Prefork
the whole friend group, basically. iykyk. π
β¨ AUTHOR β¨
Kenta Sato <karupa@cpan.org> π
π LICENSE AND COPYRIGHT π
Copyright (c) 2012, Kenta Sato. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie π β she was built on perl v5.34.0, dated 2022-07-04, and lives in the manpage as AnyEve...Manager(3pm). thanks for reading, now go fork something. π¦πΈβ¨
π¦ where this page comes from
- debian bookworm: libanyevent-forkmanager-perl 0.07-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libanyevent-forkmanager-perl 0.07-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libanyevent-forkmanager-perl 0.07-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libanyevent-forkmanager-perl 0.07-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libanyevent-forkmanager-perl 0.07-3 Β· 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