为libvirt中的每个事件调用两次事件回调函数

问题描述

newVM是创建新VM的功能。对于每个来宾VM,事件回调函数被执行2次,尽管计时器回调在每个间隔仅正确执行一次。我无法弄清楚相同的原因

eventLoopThread = None
libvirt.virEventRegisterDefaultImpl()

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

def domainEventCallback(conn,dom,event,detail,opaque):
    print(" event callback %s %s %s",dom.name(),dom.state())

def domainTimeoutCalllback(timer,opaque):
    print(" Timeout ")

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

virEventLoopNativeStart()
conn = libvirt.open('qemu:///system')
if conn == None:
    print('Failed to open connection to qemu:///system',file=sys.stderr)
    exit(1)
conn.domainEventRegister(domainEventCallback,None)
conn.setKeepAlive(5,3)

def newVM(xmldata): #xmldata is XML to define guest machine
    try: 
        conn = libvirt.open('qemu:///system')
        if conn == None:
            print('Failed to open connection to qemu:///system',file=sys.stderr)
            exit(1)

        dom = conn.defineXML(xmldata)
        if dom == None:
            print('Failed to define a domain from an XML deFinition.',file=sys.stderr)
            exit(1)

        if dom.create() < 0:
            print('Can not boot guest domain.',file=sys.stderr)
            exit(1)
    
        libvirt.virEventAddTimeout(runTime,domainTimeoutCalllback,None)
    except libvirt.libvirtError:
        abort(404,"Libvirt Could not be configured")


解决方法

在启动过程中多次调用域事件回调是正常的-每次都会给它不同的参数值。