[libvirt] [PATCH v2 2/2] Add asyncio event loop implementation

Daniel P. Berrange berrange at redhat.com
Tue Apr 4 14:27:49 UTC 2017


On Fri, Mar 17, 2017 at 02:35:53PM +0100, Wojtek Porczyk wrote:
> This is usable only on python >= 3.4 (or 3.3 with out-of-tree asyncio),
> however it should be harmless for anyone with older python versions.
> 
> In simplest case, to have the callbacks queued on the default loop:
> 
>     >>> import libvirtaio
>     >>> libvirtaio.virEventRegisterAsyncIOImpl()
> 
> The function is not present on non-compatible platforms.
> 
> Signed-off-by: Wojtek Porczyk <woju at invisiblethingslab.com>
> ---
>  libvirt-python.spec.in |   1 +
>  libvirtaio.py          | 401 +++++++++++++++++++++++++++++++++++++++++++++++++
>  sanitytest.py          |   2 +-
>  setup.py               |  12 ++
>  4 files changed, 415 insertions(+), 1 deletion(-)
>  create mode 100644 libvirtaio.py

> +    def _remove_handle(self, watch):
> +        '''Unregister a callback from a file handle.
> +
> +        :param int watch: file descriptor watch to stop listening on
> +        :returns: None (see source for explanation)
> +
> +        .. seealso::
> +            https://libvirt.org/html/libvirt-libvirt-event.html#virEventRemoveHandleFunc
> +        '''
> +        self.log.debug('remove_handle(watch=%d)', watch)
> +        callback = self.callbacks.pop(watch)
> +        assert callback is self.descriptors.remove_handle(watch)
> +        callback.close()

This code contains a bug - self.descriptors needs to be indexed by 'fd'.
We also need to delete the hash entry when the callbacks are empty.

IOW, I think the code needs to be:

        callback = self.callbacks.pop(watch)
        fd = callback.descriptor.fd
        assert callback is self.descriptors[fd].remove_handle(watch)
        if len(self.descriptors[fd].callbacks) == 0:
            del self.descriptors[fd]
        callback.close()


I'm going to send a patch series containing this fix & some other
changes / fixes. Would appreciate if you can confirm my updated
code still works from your pov.

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




More information about the libvir-list mailing list