NAME

Mojo::IOLoop::Subprocess::Sereal - Subprocesses with Sereal

SYNOPSIS

use Mojo::IOLoop::Subprocess::Sereal;

# Operation that would block the event loop for 5 seconds
my $subprocess = Mojo::IOLoop::Subprocess::Sereal->new;
$subprocess->run(
  sub {
    my $subprocess = shift;
    sleep 5;
    return '♥', 'Mojolicious';
  },
  sub {
    my ($subprocess, $err, @results) = @_;
    say "I $results[0] $results[1]!";
  }
);

# Start event loop if necessary
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;

# Run from event loop (preferred)
use Mojo::IOLoop::Subprocess::Sereal '$_subprocess';

# Arguments passed along to $subprocess->run()
my $subprocess = Mojo::IOLoop->$_subprocess(sub {...}, sub {...});

# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

DESCRIPTION

Mojo::IOLoop::Subprocess::Sereal is a subclass of Mojo::IOLoop::Subprocess which uses Sereal for data serialization. Sereal 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.

A $_subprocess method can be exported which works as a drop-in replacement for "subprocess" in Mojo::IOLoop while using Sereal for data serialization. This is the preferred interface to avoid memory leaks.

Note that Mojo::IOLoop::Subprocess is EXPERIMENTAL and thus so is this module!

ATTRIBUTES

Mojo::IOLoop::Subprocess::Sereal inherits all attributes from Mojo::IOLoop::Subprocess and implements the following new ones.

deserialize

my $cb      = $subprocess->deserialize;
$subprocess = $subprocess->deserialize(sub {...});

A callback used to deserialize subprocess return values, defaults to using Sereal::Decoder.

$subprocess->deserialize(sub {
  my $bytes = shift;
  return [];
});

serialize

my $cb      = $subprocess->serialize;
$subprocess = $subprocess->serialize(sub {...});

A callback used to serialize subprocess return values, defaults to using Sereal::Encoder.

$subprocess->serialize(sub {
  my $array = shift;
  return '';
});

METHODS

Mojo::IOLoop::Subprocess::Sereal inherits all methods from Mojo::IOLoop::Subprocess.

EXPORTS

Mojo::IOLoop::Subprocess::Sereal exports the following variables.

$_subprocess

my $subprocess = Mojo::IOLoop->$_subprocess(sub {...}, sub {...});
my $subprocess = $loop->$_subprocess(sub {...}, sub {...});

Build Mojo::IOLoop::Subprocess::Sereal object to perform computationally expensive operations in subprocesses, without blocking the event loop. Callbacks will be passed along to "run" in Mojo::IOLoop::Subprocess.

# Operation that would block the event loop for 5 seconds
Mojo::IOLoop->$_subprocess(
  sub {
    my $subprocess = shift;
    sleep 5;
    return '♥', 'Mojolicious';
  },
  sub {
    my ($subprocess, $err, @results) = @_;
    say "Subprocess error: $err" and return if $err;
    say "I $results[0] $results[1]!";
  }
);

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, Sereal