mirror of
https://gitlab.com/libvirt/libvirt-python.git
synced 2025-07-19 08:59:33 +03:00
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>
This commit is contained in:
committed by
Daniel P. Berrangé
parent
ca3731a126
commit
30f8123072
@ -211,8 +211,7 @@ class TimeoutCallback(Callback):
|
|||||||
return '<{} iden={} timeout={}>'.format(
|
return '<{} iden={} timeout={}>'.format(
|
||||||
self.__class__.__name__, self.iden, self.timeout)
|
self.__class__.__name__, self.iden, self.timeout)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def _timer(self) -> Generator[Any, None, None]:
|
||||||
def _timer(self) -> Generator[Any, None, None]:
|
|
||||||
'''An actual timer running on the event loop.
|
'''An actual timer running on the event loop.
|
||||||
|
|
||||||
This is a coroutine.
|
This is a coroutine.
|
||||||
@ -222,10 +221,10 @@ class TimeoutCallback(Callback):
|
|||||||
if self.timeout > 0:
|
if self.timeout > 0:
|
||||||
timeout = self.timeout * 1e-3
|
timeout = self.timeout * 1e-3
|
||||||
self.impl.log.debug('sleeping %r', timeout)
|
self.impl.log.debug('sleeping %r', timeout)
|
||||||
yield from asyncio.sleep(timeout)
|
await asyncio.sleep(timeout)
|
||||||
else:
|
else:
|
||||||
# scheduling timeout for next loop iteration
|
# scheduling timeout for next loop iteration
|
||||||
yield
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
self.impl.log.debug('timer %d cancelled', self.iden)
|
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'''
|
'''Schedule a ff callback from one of the handles or timers'''
|
||||||
asyncio.ensure_future(self._ff_callback(iden, opaque), loop=self.loop)
|
asyncio.ensure_future(self._ff_callback(iden, opaque), loop=self.loop)
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def _ff_callback(self, iden: int, opaque: _T) -> None:
|
||||||
def _ff_callback(self, iden: int, opaque: _T) -> None:
|
|
||||||
'''Directly free the opaque object
|
'''Directly free the opaque object
|
||||||
|
|
||||||
This is a coroutine.
|
This is a coroutine.
|
||||||
@ -316,15 +314,14 @@ class virEventAsyncIOImpl(object):
|
|||||||
libvirt.virEventInvokeFreeCallback(opaque)
|
libvirt.virEventInvokeFreeCallback(opaque)
|
||||||
self._pending_dec()
|
self._pending_dec()
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def drain(self) -> None:
|
||||||
def drain(self) -> Generator[Any, None, None]:
|
|
||||||
'''Wait for the implementation to become idle.
|
'''Wait for the implementation to become idle.
|
||||||
|
|
||||||
This is a coroutine.
|
This is a coroutine.
|
||||||
'''
|
'''
|
||||||
self.log.debug('drain()')
|
self.log.debug('drain()')
|
||||||
if self._pending:
|
if self._pending:
|
||||||
yield from self._finished.wait()
|
await self._finished.wait()
|
||||||
self.log.debug('drain ended')
|
self.log.debug('drain ended')
|
||||||
|
|
||||||
def is_idle(self) -> bool:
|
def is_idle(self) -> bool:
|
||||||
|
Reference in New Issue
Block a user