Mojolicious::Plugin::ForkCall - (DEPRECATED) run blocking code asynchronously in Mojolicious applications by forking
use Mojolicious::Lite; plugin 'Mojolicious::Plugin::ForkCall'; get '/slow' => sub { my $c = shift; my @args = ...; $c->fork_call( sub { my @args = @_; return do_slow_stuff(@args); }, [@args], sub { my ($c, @return) = @_; $c->render(json => \@return); } ); }; ... app->start;
DEPRECATED! The main module Mojo::IOLoop::ForkCall is deprecated in favor of the Mojolicious core module Mojo::IOLoop::Subprocess. This module is also deprecated but in favor of the CPAN module Mojolicious::Plugin::Subprocess which does approximately what this module does but in terms of the core module.
Registering Mojolicious::Plugin::ForkCall adds a helper method fork_call to your Mojolicious application, making it easy to start code in a forked process using Mojo::IOLoop::ForkCall.
fork_call
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.
This module adds the following helper method to your application:
$c->fork_call( sub { my @args = @_; # This code is run in a forked process return @return; }, [$arg1, $arg2, $arg3], # Optional arguments passed to the above code sub { my ($c, @return) = @_; # This code is run in the current process once the child exits } );
The fork_call helper takes up to 3 arguments: a required code reference to be run in a forked child process, an optional array reference of arguments to be passed to the child code, and a required code reference to be run in the parent as a callback. The callback is passed the controler instance and return values of the child.
The helper emulates the former Mojolicious core delay helper and as such it will render an exception (500) page if any uncaught exception occurs in the child process or in the parent callback. This also means that the parent callback will not be called if the child process encounters an exception.
delay
This helper is a convenience only and is not indended for complex cases. If you need to configure the Mojo::IOLoop::ForkCall instance or want to "fork and forget" a child, you should use the class directly rather than this helper. If more complicated delays are required, you should use the "delay" in Mojo::IOLoop method directly, along with an instance of Mojo::IOLoop::ForkCall.
To install Mojo::IOLoop::ForkCall, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::IOLoop::ForkCall
CPAN shell
perl -MCPAN -e shell install Mojo::IOLoop::ForkCall
For more information on module installation, please visit the detailed CPAN module installation guide.