Skip to content

Request: Hooks for customising awaitable conversion, to support other async libraries #6513

Open

Description

@arthur-tacca

🚀 Feature

I suggest adding hooks to allow customising the conversion between JS and Python awaitables.

Motivation

Currently, Pyodide automatically converts between asyncio and JavaScript awaitables. That means you can directly await JS async functions from Python and vice-versa:

import js
# Call JS async function from Python
result = await js.jsFn(x)
# Allow Python async function to be called from JS
js.pyFn = py_fn

But this doesn't work if you're using a different async library, e.g. Trio, because Python awaitables from different async libraries aren't compatible. I made a proof of concept port of Trio to Pyodide (source code - live demo) - it was made with an LLM so isn't directly suitable for merging into Trio, but it demonstrates it's possible. The analogous calls are:

import trio.pyodide
# Call JS function from Python 
result = await trio.pyodide.call(js.jsFn, x)
# Allow Python function to be called from JS
js.pyFn = trio.pyodide.callable_from_js(py_fn)

Pitch

Allow setting (and getting) hooks for converting JS->Python and Python->JS awaitables. The hooks would look a bit like this (with a done_callback for js_to_py to allow destroying the temporary proxies Pyodide made for the arguments):

pyodide.ffi.set_awaitable_hooks(
    js_to_py=...,  # (jsawaitable, done_callback) -> Python awaitable
    py_to_js=...,  # (pyawaitable) -> JavaScript promise
)
pyodide.ffi.get_awaitable_hooks()  # -> the current pair, like sys.get_asyncgen_hooks()

By default, they would point at the current asyncio-specific implementation, but if another async library is used then it could override the hooks, and allow the syntax seen in the first code block above.

Alternatives

  • Use a context variable for the hooks rather than a global variable. This is more flexible (e.g. it works even if you wanted to run two async libraries at the same time - but would you really want to do that?) but would need a more complex implementation (whenever you create a JS proxy from a callable Python object, you look up the hook store it in the proxy, to be called later if that hook happens to return an awaitable).
  • Put per-library conversion code in Pyodide instead of the third-party async libraries itself e.g. add support to detect if Trio is running and, if so, convert to Trio-compatible awaitables instead of asyncio.Future. Then the hook wouldn't be needed because Pyodide handles the decision itself. But this doesn't feel like the right place for conversion code to go, especially since these third-party async libraries would still need to have Pyodide-specific code in them.

Additional context

This issue was noticed in the context of PEP 818 - Adding the Core of the Pyodide Foreign Function Interface to Python. In the discussion thread, @davidbrochart noticed the problem, and it was suggested that hooks could be added for async conversion. @hoodmane, who is the PEP author, said (very reasonably) that the PEP should only include features that already exist in Pyodide, which is why I'm posting the request here.

In case it's of interest, here are some suggestions about the implementation from an LLM (Claude Fable 5.1). Feel free to ignore if AI text isn't wanted.

JavaScript to Python: one function to rework, two to reroute

Python to JavaScript: one C function and one TypeScript method

Plumbing and surface

  • Two PyObject* globals in the C core holding the installed pair, initialised to the defaults, plus a setter in the _pyodide_core module.
  • set_awaitable_hooks and get_awaitable_hooks defined in _pyodide/_core_docs.py beside run_sync at line 1701, and listed in pyodide/ffi/__init__.py's __all__.
  • Nothing in webloop.py changes. The loop keeps working as the default hooks' scheduler, and a page that installs other hooks never creates it.
  • Optional: run_sync, jsproxy.c line 4546, which also calls ensure_future, could route through py_to_js so that it can block on a non-asyncio awaitable too. Separate change.

Two things are untouched and already framework-neutral: the Python-level then, catch and finally_ overloads on a JsProxy from line 2848 onward, which attach callbacks to the raw promise without creating a future, and the typed-signature converters, which the default hook keeps applying through create_promise_handles.

Activity

  1. added
    enhancement
    on Oct 28, 2026
  2. hoodmane commented on Oct 28, 2026

    @hoodmane
    Member

    Thanks for the proposal. I'm happy to do this modulo whatever bike shedding is necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions