Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
Mojolicious::Plugin::FUseraContributed Perl DoMojolicious::Plugin::ForkCall(3)

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

SYNOPSIS
	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;

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

HELPERS
       This module adds	the following helper method to your application:

   fork_call
	$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 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.

perl v5.32.0			  2020-08-13  Mojolicious::Plugin::ForkCall(3)

NAME | SYNOPSIS | DESCRIPTION | HELPERS

Want to link to this manual page? Use this URL:
<https://www.freebsd.org/cgi/man.cgi?query=Mojolicious::Plugin::ForkCall&sektion=3&manpath=FreeBSD+12.2-RELEASE+and+Ports>

home | help