portage.util.futures._asyncio package

Module contents

class portage.util.futures._asyncio.Task(coro, loop=None)

Bases: _asyncio.Future

Schedule the execution of a coroutine: wrap it in a future. A task is a subclass of Future.

_asyncio_future_blocking
_callbacks
_cancel_message
_exception
_log_traceback
_loop
_make_cancelled_error()

Create the CancelledError to raise if the Future is cancelled.

This should only be called once when handling a cancellation since it erases the context exception value.

_repr_info()
_result
_source_traceback
_state
add_done_callback()

Add a callback to be run when the future becomes done.

The callback is called with a single argument - the future object. If the future is already done when this is called, the callback is scheduled with call_soon.

cancel(msg=None)

Cancel the future and schedule callbacks.

If the future is already done or cancelled, return False. Otherwise, change the future’s state to cancelled, schedule the callbacks and return True.

cancelled()

Return True if the future was cancelled.

done()

Return True if the future is done.

Done means either that a result / exception are available, or that the future was cancelled.

exception()

Return the exception that was set on this future.

The exception (or None if no exception was set) is returned only if the future is done. If the future has been cancelled, raises CancelledError. If the future isn’t done yet, raises InvalidStateError.

get_loop()

Return the event loop the Future is bound to.

remove_done_callback(fn, /)

Remove all instances of a callback from the “call when done” list.

Returns the number of callbacks removed.

result()

Return the result this future represents.

If the future has been cancelled, raises CancelledError. If the future’s result isn’t yet available, raises InvalidStateError. If the future is done and has an exception set, this exception is raised.

set_exception(exception, /)

Mark the future done and set an exception.

If the future is already done when this method is called, raises InvalidStateError.

set_result(result, /)

Mark the future done and set its result.

If the future is already done when this method is called, raises InvalidStateError.

portage.util.futures._asyncio._get_running_loop()
portage.util.futures._asyncio._safe_loop()

Return an event loop that’s safe to use within the current context. For portage internal callers or external API consumers calling from the main thread, this returns a globally shared event loop instance.

For external API consumers calling from a non-main thread, an asyncio loop must be registered for the current thread, or else the asyncio.get_event_loop() function will raise an error like this:

RuntimeError: There is no current event loop in thread ‘Thread-1’.

In order to avoid this RuntimeError, a loop will be automatically created like this:

asyncio.set_event_loop(asyncio.new_event_loop())

In order to avoid a ResourceWarning, automatically created loops are added to a WeakValueDictionary, and closed via an atexit hook if they still exist during exit for the current pid.

Return type

asyncio.AbstractEventLoop (or compatible)

Returns

event loop instance

portage.util.futures._asyncio._thread_weakrefs_atexit()
portage.util.futures._asyncio._wrap_loop(loop=None)

In order to deal with asyncio event loop compatibility issues, use this function to wrap the loop parameter for functions that support it. For example, since python3.4 does not have the AbstractEventLoop.create_future() method, this helper function can be used to add a wrapper that implements the create_future method for python3.4.

Parameters

loop (asyncio.AbstractEventLoop (or compatible)) – event loop

Return type

asyncio.AbstractEventLoop (or compatible)

Returns

event loop

portage.util.futures._asyncio.create_subprocess_exec(*args, **kwargs)

Create a subprocess.

Parameters
  • args (str) – program and arguments

  • stdin (file or int) – stdin file descriptor

  • stdout (file or int) – stdout file descriptor

  • stderr (file or int) – stderr file descriptor

  • close_fds (bool) – close file descriptors

  • loop (event loop) – asyncio.AbstractEventLoop (or compatible)

  • kwargs (varies) – subprocess.Popen parameters

Return type

asyncio.subprocess.Process (or compatible)

Returns

asyncio.subprocess.Process interface

portage.util.futures._asyncio.ensure_future(coro_or_future, loop=None)

Wrap a coroutine or an awaitable in a future.

If the argument is a Future, it is returned directly.

Parameters
  • coro_or_future (coroutine or Future) – coroutine or future to wrap

  • loop (asyncio.AbstractEventLoop (or compatible)) – event loop

Return type

asyncio.Future (or compatible)

Returns

an instance of Future

portage.util.futures._asyncio.get_child_watcher()

Equivalent to calling get_event_loop_policy().get_child_watcher().

portage.util.futures._asyncio.get_event_loop()

Equivalent to calling get_event_loop_policy().get_event_loop().

Return type

asyncio.AbstractEventLoop (or compatible)

Returns

the event loop for the current context

portage.util.futures._asyncio.get_event_loop_policy()

Get the current event loop policy.

Return type

asyncio.AbstractEventLoopPolicy (or compatible)

Returns

the current event loop policy

portage.util.futures._asyncio.iscoroutinefunction(func)

Return True if func is a decorated coroutine function, supporting both asyncio.coroutine and compat_coroutine since their behavior is identical for all practical purposes.

portage.util.futures._asyncio.set_child_watcher(watcher)

Equivalent to calling get_event_loop_policy().set_child_watcher(watcher).

portage.util.futures._asyncio.set_event_loop_policy(policy)

Set the current event loop policy. If policy is None, the default policy is restored.

Parameters

policy (asyncio.AbstractEventLoopPolicy or None) – new event loop policy

portage.util.futures._asyncio.sleep(delay, result=None, loop=None)

Create a future that completes after a given time (in seconds). If result is provided, it is produced to the caller when the future completes.

Parameters
  • delay (int or float) – delay seconds

  • result (object) – result of the future

  • loop (asyncio.AbstractEventLoop (or compatible)) – event loop

Return type

asyncio.Future (or compatible)

Returns

an instance of Future

portage.util.futures._asyncio.wait(futures, loop=None, timeout=None, return_when='ALL_COMPLETED')

Wraps asyncio.wait() and omits the loop argument which is not supported since python 3.10.