我可以使用带有QEMU后端的libvirt Python模块注册事件回调吗?

2024-06-16 04:25:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想编写一些代码来监视在QEMU下运行的域的事件,该域由libvirt管理。但是,尝试注册事件处理程序会产生以下错误:

>>> import libvirt
>>> conn = libvirt.openReadOnly('qemu:///system')
>>> conn.domainEventRegister(callback, None)
libvir: Remote error : this function is not supported by the connection driver: no event support

(“callback”在本例中是一个存根函数,它只打印其参数。)

我找到的关于libvirt事件处理的例子似乎并不具体说明哪些后端管理程序支持哪些功能。这对QEMU后端有用吗?在

我运行的是一个Fedora16系统,它包括libvirt0.9.6QEMUKVM0.15.1。在

对于通过<;searchengine>;找到自己的人:

更新2013-10-04

几个月后,Fedora发布了几个月,libvirtgit存储库中的event-test.py代码在fedora19上正确运行。在


Tags: 代码importnoneevent处理程序错误callback事件
1条回答
网友
1楼 · 发布于 2024-06-16 04:25:40

在注册事件之前,请确保已在libvirt事件循环中注册(或设置自己的循环)。在

libvirt源代码附带了一个很好的事件处理示例(文件名为event)-测试.py). 我附上了一个基于代码的例子

import libvirt
import time
import threading

def callback(conn, dom, event, detail, opaque):
    print "EVENT: Domain %s(%s) %s %s" % (dom.name(),
                                          dom.ID(),
                                          event,
                                          detail)

eventLoopThread = None

def virEventLoopNativeRun():
    while True:
        libvirt.virEventRunDefaultImpl()

def virEventLoopNativeStart():
    global eventLoopThread
    libvirt.virEventRegisterDefaultImpl()
    eventLoopThread = threading.Thread(target=virEventLoopNativeRun,
                                       name="libvirtEventLoop")
    eventLoopThread.setDaemon(True)
    eventLoopThread.start()

if __name__ == '__main__':

    virEventLoopNativeStart()

    conn = libvirt.openReadOnly('qemu:///system')

    conn.domainEventRegister(callback, None)
    conn.setKeepAlive(5, 3)

    while conn.isAlive() == 1:
        time.sleep(1)

祝你好运!在

//塞托

相关问题 更多 >