Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mojo::IOLoop::Subprocess->run_p() #1497

Merged
merged 1 commit into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/Mojo/IOLoop/Subprocess.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Mojo::Base 'Mojo::EventEmitter';
use Config;
use Mojo::IOLoop;
use Mojo::IOLoop::Stream;
use Mojo::Promise;
use POSIX ();
use Storable;

Expand All @@ -19,6 +20,17 @@ sub run {
return $self;
}

sub run_p {
my ($self, $child) = @_;
my $p = Mojo::Promise->new;
my $parent = sub {
my ($self, $err) = (shift, shift);
$err ? $p->reject($err) : $p->resolve(@_);
};
$self->ioloop->next_tick(sub { $self->_start($child, $parent) });
return $p;
}

sub _start {
my ($self, $child, $parent) = @_;

Expand Down Expand Up @@ -245,6 +257,24 @@ the second callback in the parent process with the results. The return values of
the first callback and exceptions thrown by it, will be serialized with
L<Storable>, so they can be shared between processes.

=head2 run_p

my $promise = $subprocess->run_p(sub {...});

Same as L</"run">, but returns a L<Mojo::Promise> object instead of accepting a
second callback.

$subprocess->run_p(sub {
kraih marked this conversation as resolved.
Show resolved Hide resolved
sleep 5;
return '♥', 'Mojolicious';
})->then(sub {
my (@results) = @_;
say "I $results[0] $results[1]!";
})->catch(sub {
my $err = shift;
say "Subprocess error: $err";
})->wait;

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
Expand Down
15 changes: 15 additions & 0 deletions t/mojo/subprocess.t
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply $result, ['♥', [{two => 2}], 3], 'right structure';

# Promises
jhthorsen marked this conversation as resolved.
Show resolved Hide resolved
$result = [];
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run_p(sub { return '♥', [{two => 2}], 3 })
->then(sub { $result = [@_] })->wait;
is_deeply $result, ['♥', [{two => 2}], 3], 'right structure';
$fail = undef;
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run_p(sub { die 'Whatever' })->catch(sub { $fail = shift })->wait;
like $fail, qr/Whatever/, 'right error';
$result = [];
Mojo::IOLoop->subprocess->run_p(sub { return '♥' })
->then(sub { $result = [@_] })->wait;
is_deeply $result, ['♥'], 'right structure';

# Event loop in subprocess
($fail, $result) = ();
$subprocess = Mojo::IOLoop::Subprocess->new;
Expand Down