[libvirt-users] Sys::Virt integration into other event loops

Daniel P. Berrange berrange at redhat.com
Fri Mar 14 15:32:45 UTC 2014


On Fri, Mar 14, 2014 at 09:01:22AM -0600, Scott Wiersdorf wrote:
> Hi all,
> 
> I’m trying to integrate Perl’s Sys::Virt into an already existing AnyEvent program.
> 
> I’m accustomed to writing things like this:
> 
> use EV;
> use AnyEvent;
> use AnyEvent::Handle;
> my $h = AnyEvent::Handle->new(fh => $fh, …);
> $h->on_read(sub { … });
> 
> EV::run;  ## start the event loop
> 
> I can add some code in the on_read() handler and every time the $fh has
> something to read, it will fire off. I’d like to do something similar
> with Sys::Virt, but I can’t seem to wrap my head around its event system.
> The only examples I can find are the ones included in Sys::Virt source,
> which consist of a series of run_once() calls, or a while loop around
> run_default().
> 
> Does anyone have any idea how I can make this play nicely with an existing
> event loop such as EV, or even to fire off ($cv->send) an AnyEvent condvar
> when the event I set in domain_event_register_any() triggers?
> 
> Our current solution involves setting an AnyEvent->timer, which periodically
> fires off and then does a run_once() to see if any events happened during
> the period, but we’d like something more responsive (i.e., triggers when
> the event actually occurs) and less poll-y feeling.

So there are a few options for integrating with libvirt's event loop

 - Use the default impl we provide by calling Sys::Virt::Event::register_default
   and Sys::Virt::Event::run_default. This is what the example program does
   and it sounds like what you're doing

 - 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.

 - Use libvirt-glib and Perl's GObject Introspection bindings to
   run the GLib2 event loop instead

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.

If you get this all working please do send back your code for it - it
would be nice to add a examples/anyevent.pl demo file for this to help
other people.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvirt-users mailing list