The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Parallel::Queue::Manager - dispatching object for Parallel::Queue.

SYNOPSIS

    # Note: examples of the options for passing in a 
    # queue via new, runqueue, and assignment 
    # are shown in t/1?-runqueue*.t.

    # arguments are the same as Parallel::Queue, 
    # other than 'export' is unnecessary as it
    # is a method.

    use Parallel::Queue::Manager;

    # whatever you use for squishification.

    use MyFile::Squash  qw( squish );

    # the input queue is a list of arguments 
    # to the $handler.
    #
    # in this case the queue is a list of >8KB
    # files that need to be squashed.

    my @pathz
    = grep
    {
        -s > 8192
    }
    glob $glob;

    # the handler takes a queue entry as argument
    # and returns a subref. in this case, it gets
    # file path and bundles it into a subref calling
    # squish.

    my $handler
    = sub
    {
        my $path    = shift;

        sub { squish $path }
    };

    # the queue can be passed as an argument to new
    # for one-stop shopping.

    Parallel::Queue::Manager
    ->new( $handler, @pathz )
    ->configure( qw( verbose finish ) )
    ->runqueue( $job_count, @pathz );

    # to simplify re-using the $qmgr object, 
    # jobs can be passed to runqueue also.
    #
    # in this case the job count can depend 
    # on whether the queued jobs are expensive
    # to process or not (e.g., gzip vs. xz -T0).

    my $qmgr 
    = Parallel::Queue::Manager
    ->new( $handler )
    ->configure( qw( finish );

    my @queue
    = map
    {
        my $task = $_;

        my $job
        = is_big_task( $_ )
        ? 1
        : 32
        ;

        [ $job, $task ]
    }
    generate_jobs;

    $qmgr->runqueue( @$_ )
    for @queue;
    
    # the "queue" and "handler" methods provide
    # access to the queue elements as lvalues.

    my $qmgr = Parallel::Queue::Manager->new();

    for( @tasks )
    {
        my( $handler, $queue ) = @$_;

        $qmgr->handler  = $handler; 
        $qmgr->runqueue( $jobs => @$queue );

        my $sanity  = $qmgr->queue;

        say join "\n\t" => 'Unfinished business:', @$sanity
        if @$sanity;
    }