Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages
Mojolicious::Plugin::ForkCall(3) User Contributed Perl Documentation Mojolicious::Plugin::ForkCall(3)

Mojolicious::Plugin::ForkCall - run blocking code asynchronously in Mojolicious applications by forking

 use Mojolicious::Lite;
 plugin 'Mojolicious::Plugin::ForkCall';
 get '/slow' => sub {
   my $c = shift;
   my @args = ...;
     sub {
       my @args = @_;
       return do_slow_stuff(@args);
     sub {
       my ($c, @return) = @_;
       $c->render(json => \@return);

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.
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:

   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 relies on the Mojolicious core helper "delay" in Mojolicious::Plugin::DefaultHelpers 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.
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 Mojolicious::Plugin::DefaultHelpers helper or "delay" in Mojo::IOLoop method directly, along with an instance of Mojo::IOLoop::ForkCall.
2019-01-02 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.