summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel P. Berrange <berrange@redhat.com>2017-04-04 14:55:46 +0100
committerDaniel P. Berrange <berrange@redhat.com>2017-04-04 15:28:50 +0100
commit58c4c12ce62e94beafd2b50805d811701852326f (patch)
tree3edb1f255cac39fc51c7ca30d9eabf393f15a24b
parent1c6503b800ac2de04372ab2ced55269e40e3149e (diff)
downloadlibvirt-python-58c4c12ce62e94beafd2b50805d811701852326f.tar.gz
event-test: add ability to run the asyncio event loop
The event test program '--loop' arg is modified to take the name of an event loop impl to run. eg 'event-test.py --loop asyncio' Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
-rwxr-xr-xexamples/event-test.py50
1 files changed, 35 insertions, 15 deletions
diff --git a/examples/event-test.py b/examples/event-test.py
index e67984f..3bca9e2 100755
--- a/examples/event-test.py
+++ b/examples/event-test.py
@@ -15,16 +15,19 @@ import errno
import time
import threading
-# For the sake of demonstration, this example program includes
-# an implementation of a pure python event loop. Most applications
-# would be better off just using the default libvirt event loop
-# APIs, instead of implementing this in python. The exception is
-# where an application wants to integrate with an existing 3rd
-# party event loop impl
+# This example can use three different event loop impls. It defaults
+# to a portable pure-python impl based on poll that is implemented
+# in this file.
#
-# Change this to 'False' to make the demo use the native
-# libvirt event loop impl
-use_pure_python_event_loop = True
+# When Python >= 3.4, it can optionally use an impl based on the
+# new asyncio module.
+#
+# Finally, it can also use the libvirt native event loop impl
+#
+# This setting thus allows 'poll', 'native' or 'asyncio' as valid
+# choices
+#
+event_impl = "poll"
do_debug = False
def debug(msg):
@@ -415,6 +418,11 @@ def virEventLoopPollRun():
global eventLoop
eventLoop.run_loop()
+def virEventLoopAIORun(loop):
+ import asyncio
+ asyncio.set_event_loop(loop)
+ loop.run_forever()
+
def virEventLoopNativeRun():
while True:
libvirt.virEventRunDefaultImpl()
@@ -427,6 +435,16 @@ def virEventLoopPollStart():
eventLoopThread.setDaemon(True)
eventLoopThread.start()
+def virEventLoopAIOStart():
+ global eventLoopThread
+ import libvirtaio
+ import asyncio
+ loop = asyncio.new_event_loop()
+ libvirtaio.virEventRegisterAsyncIOImpl(loop=loop)
+ eventLoopThread = threading.Thread(target=virEventLoopAIORun, args=(loop,), name="libvirtEventLoop")
+ eventLoopThread.setDaemon(True)
+ eventLoopThread.start()
+
def virEventLoopNativeStart():
global eventLoopThread
libvirt.virEventRegisterDefaultImpl()
@@ -650,12 +668,12 @@ def usage():
print(" uri will default to qemu:///system")
print(" --help, -h Print(this help message")
print(" --debug, -d Print(debug output")
- print(" --loop, -l Toggle event-loop-implementation")
+ print(" --loop=TYPE, -l Choose event-loop-implementation (native, poll, asyncio)")
print(" --timeout=SECS Quit after SECS seconds running")
def main():
try:
- opts, args = getopt.getopt(sys.argv[1:], "hdl", ["help", "debug", "loop", "timeout="])
+ opts, args = getopt.getopt(sys.argv[1:], "hdl:", ["help", "debug", "loop=", "timeout="])
except getopt.GetoptError as err:
# print help information and exit:
print(str(err)) # will print something like "option -a not recognized"
@@ -670,8 +688,8 @@ def main():
global do_debug
do_debug = True
if o in ("-l", "--loop"):
- global use_pure_python_event_loop
- use_pure_python_event_loop ^= True
+ global event_impl
+ event_impl = a
if o in ("--timeout"):
timeout = int(a)
@@ -680,11 +698,13 @@ def main():
else:
uri = "qemu:///system"
- print("Using uri:" + uri)
+ print("Using uri '%s' and event loop '%s'" % (uri, event_impl))
# Run a background thread with the event loop
- if use_pure_python_event_loop:
+ if event_impl == "poll":
virEventLoopPollStart()
+ elif event_impl == "asyncio":
+ virEventLoopAIOStart()
else:
virEventLoopNativeStart()