On Mar 14, 2014, at 9:32 AM, Daniel P. Berrange <berrange(a)redhat.com> wrote:
- Register and run your own event loop impl by calling the method
Sys::Virt::Event::register(), passing in a custom subclass of
the Sys::Virt::Event class. This is what you should do to integrate
with existing event loop impls like AnyEvent.
If you're set on using AnyEvent, then you want todo option 2 here. There's
no particularly good docs or example code here, but you can see how todo
this by looking at the Perl test suite. eg the t/800-events.t file.
This test suite does a pure perl event loop based on select(). You'd
probably want to adapt that and call into AnyEvent, instead of select().
The add_handle/remove_handle/update_handle/add_timeout/update_timeout/
remove_timeout methods should all call into appropriate AnyEvent APIs.
Then you just need to run AnyEvent as normal.
I’ve borrowed a little bit from t/800-events.t and have the following package:
package Sys::Virt::AnyEvent;
use parent 'Sys::Virt::Event';
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
bless $self, $class;
$self->register;
return $self;
}
sub add_handle {
my $self = shift;
my $fd = shift;
my $events = shift;
my $cb = shift;
my $opaque = shift;
my $ff = shift;
AnyEvent->io(
fh => $fd,
poll => 'r',
cb => $cb );
}
When I try to use this package (since it’s in the same lexical scope, so I don’t use()
it):
use EV;
use AnyEvent;
use Sys::Virt;
use Sys::Virt::Domain;
my $ev = Sys::Virt::AnyEvent->new();
my $c = Sys::Virt->new(uri => "remote:///system", readonly => 1);
my $w = AnyEvent->timer(after => 1, interval => 1, cb => sub { say scalar
localtime } );
my $cv; $cv = AnyEvent->condvar(cb => sub { say "cv fired: " .
$_[0]->recv;
$cv = AnyEvent->condvar(cb => __SUB__)
});
$c->domain_event_register_any(undef,
Sys::Virt::Domain::EVENT_ID_LIFECYCLE,
sub { my ($sv, $dom, $evt, $detail) = @_;
$cv->send($dom->get_name);
say STDERR "Domain: " . $dom->get_name;
say STDERR Dumper($evt) . $detail });
EV::run;
I get this:
Not a subroutine reference at
local/lib/perl5/x86_64-linux-thread-multi/AnyEvent/Impl/EV.pm line 55.
EV.pm line 55 is this:
52 sub io {
53 my ($class, %arg) = @_;
54
55 EV::io
56 $arg{fh},
57 $arg{poll} eq "r" ? EV::READ : EV::WRITE,
58 $arg{cb}
59 }
If I dump $arg{cb} it looks like this:
cb: $VAR1 = \'222528336192';
Definitely not a subroutine reference. I know there’s a lot more going on here than I’m
understanding, but I’m out of ideas of how to get Sys::Virt to play well in an AnyEvent
environment.
I see that add_handle() in t/800-events.t returns a unique watch id, but AnyEvent doesn’t
have that same notion. What should I be doing instead of returning an AnyEvent::io
object?
Scott