Subject: | How to integrate this with an app already handling its own event loop? |
I already have an app using AnyEvent, but it does its own ->recv, I'd like to kick off a Term::ReadLine loop in a callback, for example:
use AnyEvent;
use Term::ReadLine::Event;
my $cv = AE::cv;
my $term = Term::ReadLine::Event->with_AnyEvent('test');
my $input;
my $tm = AE::timer 1, 0, sub {
$input = $term->readline('Prompt >');
};
AE::cv->recv;
print "$input \n";
After 1 second I get 'Prompt >' and then
recursive blocking wait attempted at /home/jamesw/perl5/lib/perl5/Term/ReadLine/Event.pm line 34. Reading AnyEvent POD we see:
Note that doing a blocking wait in a callback is not supported by
any event loop, that is, recursive invocation of a blocking
"->recv" is not allowed, and the "recv" call will "croak" if such a
condition is detected. This condition can be slightly loosened by
using Coro::AnyEvent, which allows you to do a blocking "->recv"
from any thread that doesn't run the event loop itself.
Looking at the code you seem to be using $cv->recv as a way around not having a one_event mechanism, but it prevents the use
of $rl->readline in any callbacks (reducing its usefulness greatly)
Not sure how your original AnyEvent-only code worked, but right now AnyEvent and this module do not work together. A non-blocking,
event driven Term-ReadLine shouldn't be blocking and returning values, it should be accepting callbacks and returning a guard so it
can be cancelled, something like this perhaps:
my $rl_guard = $rl->readline_cb( sub { $input = $_[1] } );
use AnyEvent;
use Term::ReadLine::Event;
my $cv = AE::cv;
my $term = Term::ReadLine::Event->with_AnyEvent('test');
my $input;
my $tm = AE::timer 1, 0, sub {
$input = $term->readline('Prompt >');
};
AE::cv->recv;
print "$input \n";
After 1 second I get 'Prompt >' and then
recursive blocking wait attempted at /home/jamesw/perl5/lib/perl5/Term/ReadLine/Event.pm line 34. Reading AnyEvent POD we see:
Note that doing a blocking wait in a callback is not supported by
any event loop, that is, recursive invocation of a blocking
"->recv" is not allowed, and the "recv" call will "croak" if such a
condition is detected. This condition can be slightly loosened by
using Coro::AnyEvent, which allows you to do a blocking "->recv"
from any thread that doesn't run the event loop itself.
Looking at the code you seem to be using $cv->recv as a way around not having a one_event mechanism, but it prevents the use
of $rl->readline in any callbacks (reducing its usefulness greatly)
Not sure how your original AnyEvent-only code worked, but right now AnyEvent and this module do not work together. A non-blocking,
event driven Term-ReadLine shouldn't be blocking and returning values, it should be accepting callbacks and returning a guard so it
can be cancelled, something like this perhaps:
my $rl_guard = $rl->readline_cb( sub { $input = $_[1] } );