Created
April 8, 2013 11:47
-
-
Save syshack/5336239 to your computer and use it in GitHub Desktop.
There is a nice example of event handling shipped with the libvirt source (file is called event-test.py). I'm attaching an example based on that code;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment