Mojolicious::Plugin::Subprocess - Subprocesses in Mojolicious applications
use Mojolicious::Lite; plugin 'Subprocess'; get '/slow' => sub { my $c = shift; $c->subprocess(sub { return do_slow_stuff(); }, sub { my ($c, @results) = @_; $c->render(json => \@results); }); }; # or use Sereal as serializer plugin 'Subprocess' => {use_sereal => 1};
Mojolicious::Plugin::Subprocess is a Mojolicious plugin that adds a "subprocess" helper method to your application, which uses Mojo::IOLoop::Subprocess to perform computationally expensive operations in subprocesses without blocking the event loop.
The option use_sereal (requires Mojo::IOLoop::Subprocess::Role::Sereal) will use Sereal for data serialization, which is faster than Storable and supports serialization of more reference types such as Regexp. The "FREEZE/THAW CALLBACK MECHANISM" in Sereal::Encoder is supported to control serialization of blessed objects.
use_sereal
Regexp
Any other options passed to the plugin will be used as attributes to build the Mojo::IOLoop::Subprocess object.
Note that it does not increase the timeout of the connection, so if your forked process is going to take a very long time, you might need to increase that using "inactivity_timeout" in Mojolicious::Plugin::DefaultHelpers.
Mojolicious::Plugin::Subprocess implements the following helpers.
$c->subprocess(sub { my $subprocess = shift; ... }, sub { my ($c, @results) = @_; ... });
Execute the first callback in a child process with "run" in Mojo::IOLoop::Subprocess, and execute the second callback in the parent process with the results. Automatic rendering is disabled, and an exception response is rendered if an exception is thrown in either callback. This also means that the parent callback will not be called if an exception is thrown in the child callback.
Mojolicious::Plugin::Subprocess inherits all methods from Mojolicious::Plugin and implements the following new ones.
$plugin->register(Mojolicious->new); $plugin->register(Mojolicious->new, {ioloop => $ioloop});
Register helper in Mojolicious application.
Report any issues on the public bugtracker.
Dan Book <dbook@cpan.org>
This software is Copyright (c) 2016 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
Mojo::IOLoop::Subprocess, Mojolicious::Plugin::ForkCall
To install Mojolicious::Plugin::Subprocess, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Subprocess
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Subprocess
For more information on module installation, please visit the detailed CPAN module installation guide.