MooseX::Workers(3)
π MooseX::Workers π
User Contributed Perl Documentation Β· the manual page for MooseX::Workers(3)
β¨ NAME β¨
MooseX::Workers β simple sub-process management for asynchronous tasks. bestie, it's literally a babysitter for your child processes π§π
πΈ SYNOPSIS πΈ
okay so here's example number one. it prints child output normally on both STDOUT and STDERR:
EXAMPLE #1:
package Manager;
# This example prints output from the children normally on both STDOUT and STDERR
use Moose;
with qw(MooseX::Workers);
sub run {
$_[0]->spawn( sub { sleep 3; print "Hello World\n" } );
warn "Running now ... ";
POE::Kernel->run();
}
# Implement our Interface
sub worker_stdout { shift; warn join ' ', @_; }
sub worker_stderr { shift; warn join ' ', @_; }
sub worker_manager_start { warn 'started worker manager' }
sub worker_manager_stop { warn 'stopped worker manager' }
sub max_workers_reached { warn 'maximum worker count reached' }
sub worker_error { shift; warn join ' ', @_; }
sub worker_finished { warn 'a worker has finished' }
sub worker_started { shift; warn join ' ', @_; }
sub sig_child { shift; warn join ' ', @_; }
sub sig_TERM { shift; warn 'Handled TERM' }
no Moose;
Manager->new->run();
and example number two!! this one prints child output on STDERR like normal but uses STDOUT to send a hashref from the child back up to the parent π¦:
EXAMPLE #2:
package Manager;
# This example prints output from the children normally on
# STDERR but uses STDOUT to returns a hashref from the child to
# the parent
use Moose;
with qw(MooseX::Workers);
use POE qw(Filter::Reference Filter::Line);
sub run {
$_[0]->spawn(
sub {
sleep 3;
# Return a hashref (arrayref, whatever) to the parent using P::F::Reference
print @{POE::Filter::Reference->new->put([ {msg => "Hello World"} ])}; # Note the [] around the return val
# Print normally using P::F::Line (shown for
# completeness; in practice, just don't bother
# defining the _filter method
#
print STDERR "Hey look, an error message";
}
);
POE::Kernel->run();
}
# Implement our Interface
# These two are both optional; if defined (as here), they
# should return a subclass of POE::Filter.
sub stdout_filter { POE::Filter::Reference->new }
sub stderr_filter { POE::Filter::Line->new }
sub worker_stdout {
my ( $self, $result ) = @_; # $result will be a hashref: {msg => "Hello World"}
print $result->{msg};
# Note that you can do more than just print the message --
# e.g. this is the way to return data from the children for
# accumulation in the parent.
}
sub worker_stderr {
my ( $self, $stderr_msg ) = @_; # $stderr_msg will be a string: "Hey look, an error message";
warn $stderr_msg;
}
# From here down, this is identical to the previous example.
sub worker_manager_start { warn 'started worker manager' }
sub worker_manager_stop { warn 'stopped worker manager' }
sub max_workers_reached { warn 'maximum worker count reached' }
sub worker_error { shift; warn join ' ', @_; }
sub worker_finished { warn 'a worker has finished' }
sub worker_started { shift; warn join ' ', @_; }
sub sig_child { shift; warn join ' ', @_; }
sub sig_TERM { shift; warn 'Handled TERM' }
no Moose;
Manager->new->run();
π DESCRIPTION π
so MooseX::Workers is a Role that makes it stupid easy to delegate long-running tasks out into a managed child process. the whole process-management situation is handled by POE and its POE::Wheel::Run module. you just vibe, POE does the logistics. π
π METHODS π
spawn ($command)fork ($command)run_command ($command)
these three? they're literally the entire point of this module, no cap. they hand $command off to the MooseX::Worker::Engine, and that engine takes care of running $command for you.
spawn() and fork() both invoke POE::Kernel call(), which is synchronous.
run_command() invokes POE::Kernel yield(), which is asynchronous. slay.
if max_workers() has already been reached, run_command() warns and does nothing. it is up to you to re-submit $command. (if you'd rather we just run $command the second a worker frees up, see enqueue() below π)
enqueue($command)β just likerun_command(), except that ifmax_workers()has been set and that number of workers has already been reached, we drop$commandinto a FIFO command queue. the moment any running worker exits, the first$commandin the queue (if there is one) gets run. iconic.check_worker_thresholdβ this checks how many workers you have versus themax_workerslimit. it returns true if$num_workersis>=$max_workers.max_workers($count)β an accessor for the maximum number of workers. this is delegated to theMooseX::Workers::Engineobject.has_workersβ check whether we have any workers right now. delegated to theMooseX::Workers::Engineobject.num_workersβ returns the current number of workers. delegated to theMooseX::Workers::Engineobject.metaβ the Metaclass forMooseX::Workers::Engine. go read Moose's documentation for the deets π
π INTERFACE π
MooseX::Worker::Engine supports the following callbacks, bestie. memorize them, they're your new best friends:
worker_manager_startβ called when the managing session is started π«Άworker_manager_stopβ called when the managing session stopsmax_workers_reachedβ called when we reach the maximum number of workersstdout_filterβ OPTIONAL. if defined, this should return an object thatisaPOE::Filter. if it doesn't, the results are undefined. anything a child proc sends onSTDOUTgets passed through the relevant filter.stderr_filterβ OPTIONAL. if defined, this should return an object thatisaPOE::Filter. if it doesn't, the results are undefined. anything a child proc sends onSTDERRgets passed through the relevant filter.worker_stdoutβ called when a child prints toSTDOUT. if"stdout_filter"was defined, the output will be filtered appropriately, as described above. this is useful for letting child processes return data to the parent (generally viaPOE::Filter::Reference). π₯worker_stderrβ called when a child prints toSTDERR. filtered through the result of"stderr_filter"if that method is defined.worker_errorβ called when there's an error condition detected with the child πworker_finishedβ called when a worker completes$command.
if the command was a MooseX::Workers::Job, it will get the removed job instance as the first parameter.
worker_doneβ DEPRECATED
this is called before the worker is removed, so "num_workers" and "has_workers" do not reflect that a worker has just finished. use "worker_finished" instead, fr fr.
it gets the MooseX::Workers::Job instance if the $command was a job, and the POE::Wheel::Run id otherwise.
worker_startedβ called when a worker starts$commandsig_childβ called when the managing session receives aSIG CHLDeventsig_*β called when the underlying POE Kernel receives a signal. this is not limited to OS signals (ie. what you'd usually handle in Perl's%SIG), so it'll also accept arbitrary POE signals (sent viaPOE::Kernel->signal), but it does excludeSIGCHLD/SIGCHILD, which is instead handled bysig_childabove.
these interface methods get automatically inserted when MooseX::Worker::Engine detects that your manager class contains any methods beginning with sig_. signals are case-sensitive, so if you want to handle a TERM signal, you must define a sig_TERM() method. note also that this action is performed upon MooseX::Worker::Engine startup, so any run-time modification of your class which 'does' MooseX::Workers is not likely to be detected. π¬
see the sig_TERM handler in the SYNOPSIS for an example.
- see
MooseX::Workers::Enginefor more details. also seeMooseX::Workers::Jobif you'd like to give your tasks names, or set timeouts on them π
π¦ WIN32 NOTES π¦
you do not need to binmode the STDIN/STDOUT/STDERR streams in your coderefs β that's done for you. if you need utf8, it is safe to re-binmode them to :encoding(UTF-8).
coderef workers that time out are killed with a SIGINT rather than a SIGTERM, because TERM does not behave compatibly (thanks Rocco!). this is done with a:
local $SIG{INT} = sub { exit 0 };
that wraps the coderef.
β οΈ you cannot catch aTERMsent to the parent process (see"kill" in perlport, useINTinstead.
external programs are run with Win32::Job by POE::Wheel::Run. they get prepended with cmd /c so that builtin cmd commands also work. use a MooseX::Workers::Job with a string program and arrayref args for this. if you're using POE::Filter::Line with an external program (which is the default if you don't set the filter) the CRs from line ends will be removed automatically. β¨
π BUGS AND LIMITATIONS π
please report any bugs or feature requests to "bug-moosex-workers@rt.cpan.org", or through the web interface at http://rt.cpan.org.
version control: https://github.com/jhannah/moosex-workers
π AUTHORS π
- Chris Prather
"<perigrin@cpan.org>" - Tom Lanyon
"<dec@cpan.org>" - Jay Hannah
"<jay@jays.net>" - Justin Hunter
"<justin.d.hunter@gmail.com>" - David K. Storrs
"<david.storrs@gmail.com>" - Rafael Kitover
"<rkitover@cpan.org>"
π LICENCE AND COPYRIGHT π
copyright (c) 2007-2013, Chris Prather "<perigrin@cpan.org>". some rights reserved.
this module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. see perlartistic.
π¨ DISCLAIMER OF WARRANTY π¨
okay bestie, this is the part nobody reads but literally everyone should, so let's go π
Because this software is licensed free of charge, there is no warranty for it β like, to the fullest extent that applicable law allows. Unless somebody wrote it down otherwise in writing, the copyright holders and/or other parties are handing you the software "AS IS" with no warranty of any kind, whether express or implied. That includes β but is absolutely not limited to β the implied warranties of merchantability and fitness for a particular purpose. π
The entire risk as to the quality and performance of the software is on you. That's the deal. If it turns out to be defective, you're the one covering the cost of all necessary servicing, repair, or correction. no cap, that's literally what it says. π
π the liability part (read this one fr fr)
Under no circumstances β unless required by applicable law or agreed to in writing β will any copyright holder, or any other party who may modify and/or redistribute the software as permitted by the above licence, be liable to you for damages. That means general, special, incidental, or consequential damages arising out of the use or inability to use the software.
And bestie, they spelled out the receipts:
- loss of data
- data being rendered inaccurate
- losses sustained by you or third parties
- a failure of the software to operate with any other software
All of that, even if that holder or other party has been advised of the possibility of such damages. Like, even if they knew it could happen, they're still not on the hook. π«Ά
That's the whole page, my love β you're free, you're licensed, and you're on your own if it breaks. β¨
This gem of a page was brought to you by perl v5.42.0, dated 2025-07-25, and it lives in the manual as MooseX::Workers(3) π¦ go forth and fork responsibly, queen ππΈ
π¦ where this page comes from
- fedora 43: perl-MooseX-Workers 0.24-33.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms