Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mark-5/p5-broker-async

Broker tasks for multiple workers
https://github.com/mark-5/p5-broker-async

perl

Last synced: about 1 month ago
JSON representation

Broker tasks for multiple workers

Awesome Lists containing this project

README

        

# 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);
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](https://metacpan.org/pod/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.

# METHODS

## new

my @args = ( sub { ... }, ... );
my $broker = Broker::Async->new(@args);

@args must be an array of workers used for handling tasks.
Can be a code reference, a hash ref of [Broker::Async::Worker](https://metacpan.org/pod/Broker::Async::Worker) arguments, or a [Broker::Async::Worker](https://metacpan.org/pod/Broker::Async::Worker) object.
Every invocation of a worker must return a [Future](https://metacpan.org/pod/Future) object.

Under the hood, code and hash references are simply used to instantiate a [Broker::Async::Worker](https://metacpan.org/pod/Broker::Async::Worker) object.
See [Broker::Async::Worker](https://metacpan.org/pod/Broker::Async::Worker) for more documentation about how these parameters are used.

## 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](https://metacpan.org/pod/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

# LICENSE

This software is licensed under the same terms as Perl itself.