The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::Subprocess - Subprocesses in Mojolicious applications

SYNOPSIS

  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
  my $encoder = Sereal::Encoder->new;
  my $decoder = Sereal::Decoder->new;
  plugin 'Subprocess' => {
    serialize => sub { $encoder->encode($_[0]) },
    deserialize => sub { $decoder->decode($_[0]) },
  };

DESCRIPTION

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

HELPERS

Mojolicious::Plugin::Subprocess implements the following helpers.

subprocess

 $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. The callbacks are executed via "delay" in Mojolicious::Plugin::DefaultHelpers, which disables automatic rendering, keeps a reference to the transaction, and renders an exception response 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.

METHODS

Mojolicious::Plugin::Subprocess inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

 $plugin->register(Mojolicious->new);
 $plugin->register(Mojolicious->new, {ioloop => $ioloop});

Register helper in Mojolicious application.

BUGS

Report any issues on the public bugtracker.

AUTHOR

Dan Book <dbook@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2016 by Dan Book.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

SEE ALSO

Mojo::IOLoop::Subprocess, Mojolicious::Plugin::ForkCall