From nobody Wed Feb 11 09:01:52 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1491316321493152.25490396050202; Tue, 4 Apr 2017 07:32:01 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1CF25C05678E; Tue, 4 Apr 2017 14:32:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DABCE93DD9; Tue, 4 Apr 2017 14:31:59 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 937705EC6A; Tue, 4 Apr 2017 14:31:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v34EVoUi025053 for ; Tue, 4 Apr 2017 10:31:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id E047390C5E; Tue, 4 Apr 2017 14:31:50 +0000 (UTC) Received: from dhcp-17-113.lcy.redhat.com (unknown [10.42.17.113]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F0C790C6D; Tue, 4 Apr 2017 14:31:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1CF25C05678E Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1CF25C05678E From: "Daniel P. Berrange" To: libvir-list@redhat.com Date: Tue, 4 Apr 2017 15:31:34 +0100 Message-Id: <20170404143134.12141-8-berrange@redhat.com> In-Reply-To: <20170404143134.12141-1-berrange@redhat.com> References: <20170404143134.12141-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Cc: Wojtek Porczyk Subject: [libvirt] [PATCH v3 7/7] event-test: add ability to run the asyncio event loop X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Tue, 04 Apr 2017 14:32:00 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 --- examples/event-test.py | 50 +++++++++++++++++++++++++++++++++++-----------= ---- 1 file 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 =20 -# 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 =3D True +# When Python >=3D 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 =3D "poll" =20 do_debug =3D False def debug(msg): @@ -415,6 +418,11 @@ def virEventLoopPollRun(): global eventLoop eventLoop.run_loop() =20 +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() =20 +def virEventLoopAIOStart(): + global eventLoopThread + import libvirtaio + import asyncio + loop =3D asyncio.new_event_loop() + libvirtaio.virEventRegisterAsyncIOImpl(loop=3Dloop) + eventLoopThread =3D threading.Thread(target=3DvirEventLoopAIORun, args= =3D(loop,), name=3D"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=3DTYPE, -l Choose event-loop-implementation (native= , poll, asyncio)") print(" --timeout=3DSECS Quit after SECS seconds running") =20 def main(): try: - opts, args =3D getopt.getopt(sys.argv[1:], "hdl", ["help", "debug"= , "loop", "timeout=3D"]) + opts, args =3D getopt.getopt(sys.argv[1:], "hdl:", ["help", "debug= ", "loop=3D", "timeout=3D"]) except getopt.GetoptError as err: # print help information and exit: print(str(err)) # will print something like "option -a not recogni= zed" @@ -670,8 +688,8 @@ def main(): global do_debug do_debug =3D True if o in ("-l", "--loop"): - global use_pure_python_event_loop - use_pure_python_event_loop ^=3D True + global event_impl + event_impl =3D a if o in ("--timeout"): timeout =3D int(a) =20 @@ -680,11 +698,13 @@ def main(): else: uri =3D "qemu:///system" =20 - print("Using uri:" + uri) + print("Using uri '%s' and event loop '%s'" % (uri, event_impl)) =20 # Run a background thread with the event loop - if use_pure_python_event_loop: + if event_impl =3D=3D "poll": virEventLoopPollStart() + elif event_impl =3D=3D "asyncio": + virEventLoopAIOStart() else: virEventLoopNativeStart() =20 --=20 2.9.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list