pyodide.webloop#
Classes:
|
A |
|
Inherits from both |
|
A custom event loop for use in Pyodide. |
A simple event loop policy for managing |
- class pyodide.webloop.PyodideFuture(*, loop=None)#
A
Future
with extrathen()
,catch()
, andfinally_()
methods based on the Javascript promise API.create_future()
returns these so in practice all futures encountered in Pyodide should be an instance ofPyodideFuture
.- catch(onrejected)#
Equivalent to
then(None, onrejected)
- Parameters:
onrejected (
Callable
[[BaseException
],object
]) –- Return type:
- finally_(onfinally)#
When the future is either resolved or rejected, call
onfinally
with no arguments.- Parameters:
- Return type:
- 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:
onfulfilled (
Optional
[Callable
[[T
],Union
[S
,Awaitable
[S
]]]]) – A function called if the Future is fulfilled. This function receives one argument, the fulfillment value.onrejected (
Optional
[Callable
[[BaseException
],Union
[S
,Awaitable
[S
]]]]) – A function called if the Future is rejected. This function receives one argument, the rejection value.
- Return type:
- 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
andPyodideFuture
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()
andrun_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.