Replies: 2 comments 2 replies
-
For most purposes, an async function in Python is interchangeable with a regular function that returns an awaitable object. You can write those in nanobind, but there aren't any nice interfaces currently for implementing them using C++ coroutines or similar; you need to either delegate to an existing awaitable object, or laboriously implement a state machine by hand. Note that your example wrapper is extremely inefficient; it will use 100% CPU until the promise is resolved, because it just spins checking in a loop, yielding to other tasks but never going to sleep. You can make it better by integrating with your event loop's facilities for event notification; for example, if you're using asyncio:
This is a synchronous function that returns an awaitable object. It's just adapting between your promise interface and asyncio's future interface. Call it like You can write a C++ version too:
|
Beta Was this translation helpful? Give feedback.
-
@wjakob any comment please? |
Beta Was this translation helpful? Give feedback.
-
I am creating a library of Actor Model in C++ and using nanobind for the Python bindings.
I blatantly copied the model of async promises of JavaScript.
I am using callbacks (one for success, another for failure).
Something like:
I later realized that by using a wrapper that provides
__await__
I could rewrite it as:value = await wrapper_with_await(actor.send(message))
For reference, here is the code of the wrapper:
Is it possible to create an async function directly from nanobind? Thanks.
Beta Was this translation helpful? Give feedback.
All reactions