From 30f8123072a8b665abcbd5d49c1ec69accd4f58c Mon Sep 17 00:00:00 2001 From: Chris Gunn Date: Mon, 16 May 2022 17:56:50 +0100 Subject: libvirtaio: convert to using 'async' / 'await' syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'async' keyword is new in Python 3.5, as a way to declare that a method is a coroutine. This replaces the '@asyncio.coroutine' decorator that is deprecated since 3.8 and scheduled to be removed in 3.11 The 'await' keyword has to be used instead of 'yield' from any coroutines declared with 'async'. Signed-off-by: Chris Gunn [DB: Split off from a larger patch mixing multiple changes] Signed-off-by: Daniel P. Berrangé --- libvirtaio.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/libvirtaio.py b/libvirtaio.py index a24d9d4..31a8f48 100644 --- a/libvirtaio.py +++ b/libvirtaio.py @@ -211,8 +211,7 @@ class TimeoutCallback(Callback): return '<{} iden={} timeout={}>'.format( self.__class__.__name__, self.iden, self.timeout) - @asyncio.coroutine - def _timer(self) -> Generator[Any, None, None]: + async def _timer(self) -> Generator[Any, None, None]: '''An actual timer running on the event loop. This is a coroutine. @@ -222,10 +221,10 @@ class TimeoutCallback(Callback): if self.timeout > 0: timeout = self.timeout * 1e-3 self.impl.log.debug('sleeping %r', timeout) - yield from asyncio.sleep(timeout) + await asyncio.sleep(timeout) else: # scheduling timeout for next loop iteration - yield + await asyncio.sleep(0) except asyncio.CancelledError: self.impl.log.debug('timer %d cancelled', self.iden) @@ -306,8 +305,7 @@ class virEventAsyncIOImpl(object): '''Schedule a ff callback from one of the handles or timers''' asyncio.ensure_future(self._ff_callback(iden, opaque), loop=self.loop) - @asyncio.coroutine - def _ff_callback(self, iden: int, opaque: _T) -> None: + async def _ff_callback(self, iden: int, opaque: _T) -> None: '''Directly free the opaque object This is a coroutine. @@ -316,15 +314,14 @@ class virEventAsyncIOImpl(object): libvirt.virEventInvokeFreeCallback(opaque) self._pending_dec() - @asyncio.coroutine - def drain(self) -> Generator[Any, None, None]: + async def drain(self) -> None: '''Wait for the implementation to become idle. This is a coroutine. ''' self.log.debug('drain()') if self._pending: - yield from self._finished.wait() + await self._finished.wait() self.log.debug('drain ended') def is_idle(self) -> bool: -- cgit v1.2.1