View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Adam Thomason > Gearman-WorkerSpawner > Gearman::WorkerSpawner



Annotate this POD


Open  1
View/Report Bugs
Module Version: 2.17   Source  


Gearman::WorkerSpawner - Subprocess manager for Gearman workers in a Danga::Socket environment


    # write client code in some Danga::Socket environment, e.g. Perlbal:

    my $worker_manager = Gearman::WorkerSpawner->new;

    # add one or more workers
        class        => 'AdditionWorker',
        num_workers  => 4,
        config       => {
            left_hand => 3,
    $worker_manager->run_method(adder => { right_hand => 3 }, sub {
        my $return = shift;
        print $return->{sum};

    # and in the worker:

    package MethodWorker;
    use base 'Gearman::WorkerSpawner::BaseWorker';

    sub new {
        my MethodWorker $self = fields::new(shift);

        $self->register_method(adder => \&add);
        return $self;

    sub add {
        my MethodWorker $self = shift;
        my $args = shift;
        return { sum => $self->{config}{left_hand} + $args->{right_hand} };


Launches subclasses of Gearman::Worker in their own processes for communication with a gearmand. External Gearman servers may be used, or one can be created for the lifetime of the spawner.




Add a new worker set to the manager. A new supervisor process will be created to manage it if one does not already exist for the worker class. Can take the following parameters:

  • class

    (Required) The package name of the Gearman::Worker subclass which will register itself for work when instantiated. This need not be distinct across different calls.

  • source

    (Optional) The path to the file containing the definition of 'class'; only necessary if the module can't be use'd for some reason.

  • caller_source

    (Optional) If true, assume that the source for 'class' is the calling module or script. This will generally fail if the working directory has changed since program startup. This overrides source if both are provided.

  • num_workers

    The number of worker children to spawn. If any child processes die they will be respawned. Defaults to 1.

  • config

    An opaque data structure to pass to the child process, generally used to keep configuration that is specific to the worker but not any one job. Must be serializable via Storable.


Returns only once all worker are ready to accept jobs. This will only wait on workers which have been started since the last call to wait_until_all_ready.

$spawner->add_task($funcname, $arg, \%options)

Asynchronously submits a task to a configured Gearman server. May either take a Gearman::Task object, or the 3 arguments that the Gearman::Task constructor takes.

$spawner->run_method($funcname, $arg, \%options)
$spawner->run_method($funcname, $arg, $callback)

Submits a task but with less boilerplate than add_task. %options is the same as for add_task. Marshaling of $arg is done for you in a manner compatible with methods created with Gearman::WorkerSpawner::BaseWorker::register_method. The on_fail handler will be called if marshalling fails for some reason.

If the second form is used, an empty %options is created and $callback is used as the on_complete handler.


Accessor for the suffix which is appended to the method name. Defaults to '_m'.


Tell all spawned processes to quit (by default, with SIGINT).


Upon destruction, stop_workers is called unless you've already called it.


Returns an arrayref of server host:port specs. If an 'auto' server was requested, its hostspec is included.



Returns the Gearman::Client::Async object used by the spawner.

Gearman::WorkerSpawner->_supervise('My::WorkerClass', @ARGV)

Loads the given Gearman::Worker subclass, then parses additional arguments as specified by the return value of the worker class' options() class method via Getopt::Long. These options are passed to the worker object's constructor and the work method of the worker object is called repeatedly until either SIG_INT is received or the ppid changes (parent went away).

This class method is automatically executed if Gearman/ has no caller(), i.e. if it is run as a script rather than loaded as a module. This should only be done by other internal methods of this package (add_worker).








brian d foy's modulino article:


Adam Thomason, <>


Copyright (C) 2007-2011 by Say Media Inc, <>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.

syntax highlighting: