Skip to content

Return asyncio awaitable object for Gio async routines

Benjamin Berg requested to merge wip/gio-async-awaitable-return into master

Features/Notes:

  • If no callback is given, an awaitable object is returned
  • If cancellable is given, this will be used, otherwise one will be created implicitly
  • user_data will be ignored if callback is not set
  • If *_finish throws an exception, and the result is not fetched a message will be logged.

TODO:

How it works:

  • During function cache creation, async routines are detected, this means:

    • One callback of type GAsyncReadyCallback which has a closure that is GI_SCOPE_TYPE_ASYNC
    • A single cancellable parameter
    • No return/out values
    • A _finish function exists (type is not checked, but I think that is acceptable as it will raise an exception when called).
    • Save information in GIIntrospection
  • If during argument dispatch, we find that the GI_SCOPE_TYPE_ASYNC callback on such a function has not been provided, we:

    • Create a new PyGIAsync instance, and keep it in the scope to return
    • Retrieve the cancellable argument
    • Pass PyGIAsync instance as user_data and set specific callback
  • PyGIAsync instance implements awaitable protocol, with the following caveats/differences:

    • In python, a cancellation means the future is done immediately. This will not be the case for PyGIAsync, we will always wait for the callback method to be called.
    • Right now iteration isn't quite correct; this is only relevant when the API is used incorrectly in ways that make no sense
Edited by Benjamin Berg

Merge request reports