Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
$subprocess looks better in examples
  • Loading branch information
kraih committed Aug 27, 2016
1 parent 667812e commit eddf9be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
14 changes: 7 additions & 7 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -147,9 +147,9 @@ sub stream {
}

sub subprocess {
my $sp = Mojo::IOLoop::Subprocess->new;
weaken $sp->ioloop(_instance(shift))->{ioloop};
return $sp->run(@_);
my $subprocess = Mojo::IOLoop::Subprocess->new;
weaken $subprocess->ioloop(_instance(shift))->{ioloop};
return $subprocess->run(@_);
}

sub timer { shift->_timer(timer => @_) }
Expand Down Expand Up @@ -604,8 +604,8 @@ Get L<Mojo::IOLoop::Stream> object for id or turn object into a connection.
=head2 subprocess
my $sp = Mojo::IOLoop->subprocess(sub {...}, sub {...});
my $sp = $loop->subprocess(sub {...}, sub {...});
my $subprocess = Mojo::IOLoop->subprocess(sub {...}, sub {...});
my $subprocess = $loop->subprocess(sub {...}, sub {...});
Build L<Mojo::IOLoop::Subprocess> object to perform computationally expensive
operations in subprocesses, without blocking the event loop. Callbacks will be
Expand All @@ -615,12 +615,12 @@ EXPERIMENTAL and might change without warning!
# Operation that would block the event loop for 5 seconds
Mojo::IOLoop->subprocess(
sub {
my $sp = shift;
my $subprocess = shift;
sleep 5;
return '♥', 'Mojolicious';
},
sub {
my ($sp, $err, @results) = @_;
my ($subprocess, $err, @results) = @_;
say "I $results[0] $results[1]!";
}
);
Expand Down
30 changes: 15 additions & 15 deletions lib/Mojo/IOLoop/Subprocess.pm
Expand Up @@ -59,21 +59,21 @@ Mojo::IOLoop::Subprocess - Subprocesses
use Mojo::IOLoop::Subprocess;
# Operation that would block the event loop for 5 seconds
my $sp = Mojo::IOLoop::Subprocess->new;
$sp->run(
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub {
my $sp = shift;
my $subprocess = shift;
sleep 5;
return '♥', 'Mojolicious';
},
sub {
my ($sp, $err, @results) = @_;
my ($subprocess, $err, @results) = @_;
say "I $results[0] $results[1]!";
}
);
# Start event loop if necessary
$sp->ioloop->start unless $sp->ioloop->is_running;
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;
=head1 DESCRIPTION
Expand All @@ -87,33 +87,33 @@ L<Mojo::IOLoop::Subprocess> implements the following attributes.
=head2 deserialize
my $cb = $sp->deserialize;
$sp = $sp->deserialize(sub {...});
my $cb = $subprocess->deserialize;
$subprocess = $subprocess->deserialize(sub {...});
A callback used to deserialize subprocess return values, defaults to using
L<Storable>.
$sessions->deserialize(sub {
$subprocess->deserialize(sub {
my $bytes = shift;
return [];
});
=head2 ioloop
my $loop = $sp->ioloop;
$sp = $sp->ioloop(Mojo::IOLoop->new);
my $loop = $subprocess->ioloop;
$subprocess = $subprocess->ioloop(Mojo::IOLoop->new);
Event loop object to control, defaults to the global L<Mojo::IOLoop> singleton.
=head2 serialize
my $cb = $sp->serialize;
$sp = $sp->serialize(sub {...});
my $cb = $subprocess->serialize;
$subprocess = $subprocess->serialize(sub {...});
A callback used to serialize subprocess return values, defaults to using
L<Storable>.
$sessions->serialize(sub {
$subprocess->serialize(sub {
my $array = shift;
return '';
});
Expand All @@ -125,13 +125,13 @@ implements the following new ones.
=head2 pid
my $pid = $sp->pid;
my $pid = $subprocess->pid;
Process id of the spawned subprocess if available.
=head2 run
$sp = $sp->run(sub {...}, sub {...});
$subprocess = $subprocess->run(sub {...}, sub {...});
Execute the first callback in a child process and wait for it to return one or
more values, without blocking L</"ioloop"> in the parent process. Then execute
Expand Down
32 changes: 16 additions & 16 deletions t/mojo/subprocess.t
Expand Up @@ -12,26 +12,26 @@ use Mojo::IOLoop::Subprocess;

# Huge result
my ($fail, $result);
my $sp = Mojo::IOLoop::Subprocess->new;
$sp->run(
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub { shift->pid . $$ . ('x' x 100000) },
sub {
my ($sp, $err, $two) = @_;
my ($subprocess, $err, $two) = @_;
$fail = $err;
$result = $two;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result, 0 . $sp->pid . ('x' x 100000), 'right result';
is $result, 0 . $subprocess->pid . ('x' x 100000), 'right result';

# Multiple return values
($fail, $result) = ();
$sp = Mojo::IOLoop::Subprocess->new;
$sp->run(
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub { return '', [{two => 2}], 3 },
sub {
my ($sp, $err, @results) = @_;
my ($subprocess, $err, @results) = @_;
$fail = $err;
$result = \@results;
}
Expand All @@ -42,16 +42,16 @@ is_deeply $result, ['♥', [{two => 2}], 3], 'right structure';

# Event loop in subprocess
($fail, $result) = ();
$sp = Mojo::IOLoop::Subprocess->new;
$sp->run(
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub {
my $result;
Mojo::IOLoop->next_tick(sub { $result = 23 });
Mojo::IOLoop->start;
return $result;
},
sub {
my ($sp, $err, $twenty_three) = @_;
my ($subprocess, $err, $twenty_three) = @_;
$fail = $err;
$result = $twenty_three;
}
Expand Down Expand Up @@ -82,21 +82,21 @@ $fail = undef;
Mojo::IOLoop::Subprocess->new->run(
sub { exit 3 },
sub {
my ($sp, $err) = @_;
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Mojo::IOLoop->start;
is $fail, 'Non-zero exit status (3)', 'right error';

# Serialization error
$fail = undef;
$sp = Mojo::IOLoop::Subprocess->new;
$sp->deserialize(sub { die 'Whatever' });
$sp->run(
$fail = undef;
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->deserialize(sub { die 'Whatever' });
$subprocess->run(
sub { 1 + 1 },
sub {
my ($sp, $err) = @_;
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Expand Down

0 comments on commit eddf9be

Please sign in to comment.