pyodide.webloop#

Classes:

PyodideFuture(*[, loop])

A Future with extra then(), catch(), and finally_() methods based on the Javascript promise API.

PyodideTask(coro, *[, loop, name, context, ...])

Inherits from both Task and PyodideFuture

WebLoop()

A custom event loop for use in Pyodide.

WebLoopPolicy()

A simple event loop policy for managing WebLoop-based event loops.

class pyodide.webloop.PyodideFuture(*, loop=None)#

A Future with extra then(), catch(), and finally_() methods based on the Javascript promise API. create_future() returns these so in practice all futures encountered in Pyodide should be an instance of PyodideFuture.

catch(onrejected)#

Equivalent to then(None, onrejected)

Parameters:

onrejected (Callable[[BaseException], object]) –

Return type:

PyodideFuture[Any]

finally_(onfinally)#

When the future is either resolved or rejected, call onfinally with no arguments.

Parameters:

onfinally (Callable[[], None]) –

Return type:

PyodideFuture[T]

then(onfulfilled, onrejected=None)#

When the Future is done, either execute onfulfilled with the result or execute onrejected with the exception.

Returns a new Future which will be marked done when either the onfulfilled or onrejected callback is completed. If the return value of the executed callback is awaitable it will be awaited repeatedly until a nonawaitable value is received. The returned Future will be resolved with that value. If an error is raised, the returned Future will be rejected with the error.

Parameters:
Return type:

PyodideFuture[S]

Returns:

A new future to be resolved when the original future is done and the appropriate callback is also done.

class pyodide.webloop.PyodideTask(coro, *, loop=None, name=None, context=None, eager_start=False)#

Inherits from both Task and PyodideFuture

Instantiation is discouraged unless you are writing your own event loop.

class pyodide.webloop.WebLoop#

A custom event loop for use in Pyodide.

Schedules tasks on the browser event loop. Does no lifecycle management and runs forever.

run_forever() and run_until_complete() cannot block like a normal event loop would because we only have one thread so blocking would stall the browser event loop and prevent anything from ever happening.

We defer all work to the browser event loop using the setTimeout() function. To ensure that this event loop doesn’t stall out UI and other browser handling, we want to make sure that each task is scheduled on the browser event loop as a task not as a microtask. setTimeout(callback, 0) enqueues the callback as a task so it works well for our purposes.

See the Python Event Loop documentation.

class pyodide.webloop.WebLoopPolicy#

A simple event loop policy for managing WebLoop-based event loops.