GPUPromise

class wgpu.GPUPromise

Bases: GPUPromise

A GPUPromise represents the eventual result of an asynchronous wgpu operation.

A GPUPromise is a bit like an asyncio.Future, but specific for wgpu, and with an API more similar to JavaScript’s Promise.

Some methods of the wgpu API are asynchronous. They return a GPUPromise, which provides a few different ways handle it:

  • It can be awaited using await promise. This is the “cleanest” way, but can only be used from a co-routine (i.e. an async code path).

  • A callback can be registered using promise.then(callback), which will be called when the promise resolves. A new promise is returned.

  • You can sync-wait for it, using promise.sync_wait(). This is simple, but makes code less portable (does not work on Pyodide backend) and potentially slower.

A GPUPromise is in one of these states:

  • “pending”: initial state, neither fulfilled nor rejected.

  • “pending-fulfilled”: the input has been set, but the promise has not yet been resolved.

  • “pending-rejected”: the error has been set, but the promise has not yet been resolved.

  • “fulfilled”: meaning that the operation was completed successfully.

  • “rejected”: meaning that the operation failed.

catch(callback: Callable[[Exception], None] | None)

Set a callback that will be called when the promise is rejected.

The callback will receive one argument: the error object.

sync_wait() AwaitedType

Synchronously wait for the promise to resolve and return the result.

Note that this method should be avoided in event callbacks, since it can make them slow.

Note that this method may not be supported by all backends (e.g. the upcoming JavaScript/Pyodide one), and using it will make your code less portable.

then(callback: Callable[[AwaitedType], None], error_callback: Callable[[Exception], None] | None = None, title: str | None = None)

Set a callback that will be called when the promise is fulfilled.

The callback will receive one argument: the result of the promise.