Scroll to navigation

Mojo::IOLoop::ReadWriteProcess::Queue(3pm) User Contributed Perl Documentation Mojo::IOLoop::ReadWriteProcess::Queue(3pm)

NAME

Mojo::IOLoop::ReadWriteProcess::Queue - Queue for Mojo::IOLoop::ReadWriteProcess objects.

SYNOPSIS

    use Mojo::IOLoop::ReadWriteProcess qw(queue process);
    my $n_proc = 20;
    my $fired;
    my $q = queue;
    $q->pool->maximum_processes(2); # Max 2 processes in parallel
    $q->queue->maximum_processes(10); # Max queue is 10
    $q->add( process sub { return 42 } ) for 1..7;
    # Subscribe to all "stop" events in the pool
    $q->once(stop => sub { $fired++; });
    # Consume the queue
    $q->consume();
    my $all = $q->done; # All processes, Mojo::Collection of Mojo::IOLoop::ReadWriteProcess
    # Set your own running pool
    $q->pool(parallel sub { return 42 } => 5);
    # Set your own queue
    $q->queue(parallel sub { return 42 } => 20);
    $q->consume();

METHODS

Mojo::IOLoop::ReadWriteProcess::Queue inherits all methods from Mojo::Base and implements the following new ones. Note: It proxies all the other methods of Mojo::IOLoop::ReadWriteProcess for the whole process group.

add

    use Mojo::IOLoop::ReadWriteProcess qw(queue process);
    my $q = queue();
    $q->add(sub { print "Hello 2! " });
    $q->add(process sub { print "Hello 2! " });

Add the process to the queue.

consume

    use Mojo::IOLoop::ReadWriteProcess qw(queue);
    my $q = queue();
    $q->add(sub { print "Hello 2! " });
    $q->add(process sub { print "Hello 2! " });
    $q->consume; # executes and exhaust the processes

Starts the processes and empties the queue. Note: maximum_processes can be set both to the pool (number of process to be run in parallel), and for the queue (that gets exhausted during the consume() phase).

    $q->pool->maximum_processes(2); # Max 2 processes in parallel
    $q->queue->maximum_processes(10); # Max queue is 10

exhausted

    use Mojo::IOLoop::ReadWriteProcess qw(queue);
    my $q = queue();
    $q->add(sub { print "Hello 2! " });
    $q->add(process sub { print "Hello 2! " });
    $q->consume; # executes and exhaust the processes
    $q->exhausted; # 1

Returns 1 if the queue is exhausted.

ENVIRONMENT

You can set the MOJO_PROCESS_MAXIMUM_PROCESSES environment variable to specify the the maximum number of processes allowed in the pool and the queue, that are Mojo::IOLoop::ReadWriteProcess::Pool instances.

    MOJO_PROCESS_MAXIMUM_PROCESSES=10000

LICENSE

Copyright (C) Ettore Di Giacinto.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Ettore Di Giacinto <edigiacinto@suse.com>

2024-03-08 perl v5.38.2