wgpu-py icon indicating copy to clipboard operation
wgpu-py copied to clipboard

Support for callbacks in async functions

Open almarklein opened this issue 11 months ago • 0 comments

For the async API functions in wgpu, we currently offer two options: busy-wait for it, or async-wait for it.

In JS, an async function returns a promise, and this offers a 3d option: to provide a callback that is called when the promise resolves. I had completely overlooked this option. It's interesting, because its an option that works in sync (not-async) code, and can be made to work the same way in Python and JS/Pyodide.

I probably need to think about this more, also how this plays with rendercanvas and pygfx, but what I currently think would be a good plan is to have all the async API functions return a Promise-like object, which can then be used to:

  • promise.busy_wait() - Python only, for code that does not care waiting a bit
  • await promise - works in Python and JS
  • promise.then(callback) - Python and JS

(we already have an object that does the first two things, but it's used internally (not public) and called WgpuAwaitable)

almarklein avatar Mar 14 '25 11:03 almarklein