AnyEvent-Subprocess
view release on metacpan or search on metacpan
examples/synopsis.pl view on Meta::CPAN
say "The child said: $line";
});
# write to the child's stdin
$run->delegate('stdin')->handle->push_write("Hello, world!\n");
# close stdin after it has been written to the child
$run->delegate('stdin')->handle->on_drain(sub { $_[0]->close_fh });
# kill the child if it takes too long to produce a result
$killer = AnyEvent->timer( after => 2, interval => 0, cb => sub {
warn "TOO SLOW. BAI.";
$run->kill(2); # SIGINT.
});
$cv->cb( sub { $cv->recv; exit 0 } );
# ensure the event loop runs until the on_completion handler dies
EV::loop(); # you can use any AnyEvent-compatible event loop, including POE
# eventually prints "the child said: got line: hello, world!", or
# perhaps dies if your system is really really overloaded.
lib/AnyEvent/Subprocess.pm view on Meta::CPAN
say "The child said: $line";
});
# write to the child's stdin
$run->delegate('stdin')->handle->push_write("Hello, world!\n");
# close stdin after it has been written to the child
$run->delegate('stdin')->handle->on_drain(sub { $_[0]->close_fh });
# kill the child if it takes too long to produce a result
my $killer = AnyEvent->timer( after => 42, interval => 0, cb => sub {
$run->kill(2); # SIGINT.
});
# ensure the event loop runs until the on_completion handler dies
EV::loop(); # you can use any AnyEvent-compatible event loop, including POE
# eventually prints "The child said: Got line: Hello, world!", or
# perhaps dies if your system is really really overloaded.
=head1 DESCRIPTION
lib/AnyEvent/Subprocess/Job/Delegate/Timeout.pm view on Meta::CPAN
has 'child' => (
init_arg => undef,
accessor => 'child',
);
sub build_run_delegates {
my $self = shift;
my $run; $run = AnyEvent::Subprocess::Running::Delegate::Timeout->new(
name => $self->name,
timer => AnyEvent->timer( after => $self->time_limit, interval => 0, cb => sub {
$run->killed_by_timer(1);
$self->child->kill( $self->kill_with );
$run->clear_timer;
}),
);
return $run;
}
sub parent_setup_hook {
my ($self, $job, $run) = @_;
$self->child($run);
}
lib/AnyEvent/Subprocess/Running/Delegate/Timeout.pm view on Meta::CPAN
BEGIN {
$AnyEvent::Subprocess::Running::Delegate::Timeout::VERSION = '1.102912';
}
# ABSTRACT: Running part of Timeout delegate
use Moose;
use namespace::autoclean;
use AnyEvent::Subprocess::Done::Delegate::Timeout;
with 'AnyEvent::Subprocess::Running::Delegate';
has 'timer' => (
is => 'ro',
clearer => 'clear_timer',
);
has 'killed_by_timer' => (
init_arg => undef,
accessor => 'killed_by_timer',
default => sub { undef },
);
sub completion_hook {
my $self = shift;
$self->clear_timer;
}
sub build_done_delegates {
my $self = shift;
return AnyEvent::Subprocess::Done::Delegate::Timeout->new(
name => $self->name,
timed_out => $self->killed_by_timer,
);
}
sub build_events {}
sub build_code_args {}
__PACKAGE__->meta->make_immutable;
1;
t/pty-basic.t view on Meta::CPAN
$joiner->send_event( 'echoed_input' );
} );
$run->delegate('comm')->handle->push_read( line => sub {
my ($h, $line, $eol) = @_;
is $line, 'got line: {this is a test}', 'process got input and cooked it';
$joiner->send_event( 'cooked_input' );
$run->delegate('comm')->handle->close_fh;
} );
my $timeout = AnyEvent->timer( after => 5, cb => sub {
diag "test subprocess failed to exit; this is strange";
done_testing;
exit 0;
});
my $done = $completion_condvar->recv();
undef $timeout;
isa_ok $done, 'AnyEvent::Subprocess::Done';
t/timeout.t view on Meta::CPAN
use AnyEvent::Subprocess;
{
my $proc = AnyEvent::Subprocess->new(
delegates => [{ Timeout => { timeout => 1 } }, 'CompletionCondvar'],
code => sub { sleep 10 },
);
my $run = $proc->run;
my $run_timer = $run->delegate('timeout');
ok $run_timer->timer, 'has timer';
ok !$run_timer->killed_by_timer, 'not killed by timer yet';
my $done = $run->delegate('completion_condvar')->condvar->recv;
ok $done->delegate('timeout')->timed_out, 'timed out';
ok !$done->is_success, 'was not a success';
}
{
my $proc = AnyEvent::Subprocess->new(
delegates => [{ Timeout => { timeout => 3 } }, 'CompletionCondvar'],
code => sub { sleep 1 },
);
my $run = $proc->run;
my $run_timer = $run->delegate('timeout');
ok $run_timer->timer, 'has timer';
ok !$run_timer->killed_by_timer, 'not killed by timer yet';
my $done = $run->delegate('completion_condvar')->condvar->recv;
ok !$done->delegate('timeout')->timed_out, 'did not time out';
ok $done->is_success, 'was a success';
}
done_testing;
( run in 1.139 second using v1.01-cache-2.11-cpan-49f99fa48dc )