1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

utils/asyn: Factor out the calls to asyncio.run

Prepare for providing our own implementation of asyncio.run() to work
without nest_asyncio package.
This commit is contained in:
Douglas Raillard 2024-04-26 13:41:40 +01:00 committed by Marc Bonnici
parent 165b87f248
commit b2e19d333b
2 changed files with 14 additions and 7 deletions

View File

@ -19,13 +19,12 @@ from collections import namedtuple
from shlex import quote from shlex import quote
import itertools import itertools
import warnings import warnings
import asyncio
from devlib.module import Module from devlib.module import Module
from devlib.exception import TargetStableError from devlib.exception import TargetStableError
from devlib.utils.misc import list_to_ranges, isiterable from devlib.utils.misc import list_to_ranges, isiterable
from devlib.utils.types import boolean from devlib.utils.types import boolean
from devlib.utils.asyn import asyncf from devlib.utils.asyn import asyncf, run
class Controller(object): class Controller(object):
@ -410,7 +409,7 @@ class CgroupsModule(Module):
controller.mount_point) controller.mount_point)
self.controllers[ss.name] = controller self.controllers[ss.name] = controller
asyncio.run( run(
target.async_manager.map_concurrently( target.async_manager.map_concurrently(
register_controller, register_controller,
subsys, subsys,

View File

@ -292,6 +292,14 @@ class memoized_method:
self._name = name self._name = name
def run(coro):
"""
Similar to :func:`asyncio.run` but can be called while an event loop is
running.
"""
return asyncio.run(coro)
def asyncf(f): def asyncf(f):
""" """
Decorator used to turn a coroutine into a blocking function, with an Decorator used to turn a coroutine into a blocking function, with an
@ -328,14 +336,14 @@ def asyncf(f):
asyncgen = x.__aiter__() asyncgen = x.__aiter__()
while True: while True:
try: try:
yield asyncio.run(asyncgen.__anext__()) yield run(asyncgen.__anext__())
except StopAsyncIteration: except StopAsyncIteration:
return return
return genf() return genf()
else: else:
return await x return await x
return asyncio.run(wrapper()) return run(wrapper())
return _AsyncPolymorphicFunction( return _AsyncPolymorphicFunction(
asyn=f, asyn=f,
@ -358,10 +366,10 @@ class _AsyncPolymorphicCM:
return self.cm.__aexit__(*args, **kwargs) return self.cm.__aexit__(*args, **kwargs)
def __enter__(self, *args, **kwargs): def __enter__(self, *args, **kwargs):
return asyncio.run(self.cm.__aenter__(*args, **kwargs)) return run(self.cm.__aenter__(*args, **kwargs))
def __exit__(self, *args, **kwargs): def __exit__(self, *args, **kwargs):
return asyncio.run(self.cm.__aexit__(*args, **kwargs)) return run(self.cm.__aexit__(*args, **kwargs))
def asynccontextmanager(f): def asynccontextmanager(f):