summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Gunn <chrisgun@microsoft.com>2022-05-16 17:56:50 +0100
committerDaniel P. Berrangé <berrange@redhat.com>2022-06-08 16:43:52 +0100
commit30f8123072a8b665abcbd5d49c1ec69accd4f58c (patch)
treea6828f20dca71694ef4588616d1a40aeb3fcc809
parentca3731a126da2ed7f2c235f84daf54638e8933a5 (diff)
downloadlibvirt-python-30f8123072a8b665abcbd5d49c1ec69accd4f58c.tar.gz
libvirtaio: convert to using 'async' / 'await' syntax
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 <chrisgun@microsoft.com> [DB: Split off from a larger patch mixing multiple changes] Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
-rw-r--r--libvirtaio.py15
1 files 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: