NAME
Broker::Async - broker tasks for multiple workers
SYNOPSIS
my @workers;
for my $uri (@uris) {
my $client = SomeClient->new($uri);
push @workers, sub { $client->request(@_) };
}
my $broker = Broker::Async->new(workers => \@workers);
for my $future (map $broker->do($_), @requests) {
my $result = $future->get;
...
}
DESCRIPTION
This module brokers tasks for multiple asynchronous workers. A worker can be any code reference that returns a Future, representing work awaiting completion.
Some common use cases include throttling asynchronous requests to a server, or delegating tasks to a limited number of processes.
ATTRIBUTES
workers
An array ref of workers used for handling tasks. Can be a code reference, a hash ref of Broker::Async::Worker arguments, or a Broker::Async::Worker object. Every invocation of a worker must return a Future object.
Under the hood, code and hash references are simply used to instantiate a Broker::Async::Worker object. See Broker::Async::Worker for more documentation about how these parameters are used.
METHODS
new
my $broker = Broker::Async->new(
workers => [ sub { ... }, ... ],
);
do
my $future = $broker->do(@args);
Queue the invocation of a worker with @args. @args can be any data structure, and is passed as is to a worker code ref. Returns a Future object that resolves when the work is done.
There is no guarantee when a worker will be called, that depends on when a worker becomes available. However, calls are guaranteed to be invoked in the order they are seen by $broker->do.
AUTHOR
Mark Flickinger <maf@cpan.org>
LICENSE
This software is licensed under the same terms as Perl itself.