Mojolicious::Plugin::Parallol - Because parallel requests should be as fun as parallololololol!
# Mojolicious $self->plugin('Parallol'); # Mojolicious::Lite plugin 'Parallol';
Mojolicious::Plugin::Parallol provides a simple helper for managing several parallel requests in the controller.
Mojolicious::Plugin::Parallol implements the following helpers.
parallol
Parallol optimizes for the common case: You want to call several parallel requests and render the view when they're done.
get '/' => sub { my $self = shift; $self->ua->get('http://bbc.co.uk/', $self->parallol(sub { $self->stash(bbc => pop->res->dom->at('title')->text); })); $self->ua->get('http://mojolicio.us/', $self->parallol(sub { $self->stash(mojo => pop->res->dom->at('title')->text); })); };
By wrapping a callback in $self->parallol you mark the current response as asynchronous (see "render_later" in Mojolicious::Controller) and Parallol will render the view when all callbacks are called.
$self->parallol
By passing a string to $self->parallol it will stash the last argument of the result instead. If we rewrite the previous example to use a helper, we can simplify our controller quite a lot.
get '/' => sub { my $self = shift; $self->title('http://bbc.co.uk/', $self->parallol('bbc')); $self->title('http://mojolicio.us/', $self->parallol('mojo')); }; helper title => sub { my ($self, $url, $cb) = @_; $self->ua->get($url, sub { $cb->(pop->res->dom->at('title')->text); }); };
It's recommended that you move as much logic to helpers and other classes/methods so you can take advantage of automatic stashing.
When you need to do more than just rendering the view you can override the "done" callback:
get '/' => sub { my $self = shift; $self->on_parallol(sub { shift->render(template => 'something_else'); }); };
If the Mojo IO loop is not running, Parallol will automatically start it for your request, and stop it again when all callbacks have been called. This means that asynchronous requests just works when you use Parallol in servers that don't use Mojo's IO loop (PSGI).
In order to prevent memory leaks, Parallol will automatically weaken $self. This means that if you don't refer to $self in your callback objects will magically disappear.
weaken $self
$self
# This controller will behave very strangely: get '/' => sub { my $self = shift; my $res = {}; $self->ua->get('http://bbc.co.uk/', $self->parallol(sub { # There's no reference to $self in this block $res->{bbc} = pop->res->dom->at('title')->text; })); };
In these cases you can disabled weakening by passing in weaken => 0.
weaken => 0
# This controller is fine: get '/' => sub { my $self = shift; my $res = {}; $self->ua->get('http://bbc.co.uk/', $self->parallol(weaken => 0, sub { # There's no reference to $self in this block $res->{bbc} = pop->res->dom->at('title')->text; })); };
Mojolicious::Plugin::Parrallol inherits all methods from Mojolicious::Plugin and implements the following new ones.
register
$plugin->register;
Register helpers in Mojolicious application.
Mojolicious, Parallol
Magnus Holm mailto:magnus@nordaaker.com
This software is licensed under the same terms as Perl itself.
To install Parallol, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parallol
CPAN shell
perl -MCPAN -e shell install Parallol
For more information on module installation, please visit the detailed CPAN module installation guide.