Skip to content

Commit

Permalink
Add documentation for pyawaitable_await_function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Jun 22, 2024
1 parent 7190b3f commit 8cb78fa
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/adding_coros.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,38 @@ spam(PyObject *self, PyObject *args)
This would be equivalent to `await foo` from Python.
Alternatively, you can use `pyawaitable_await_function` (`PyAwaitable_AwaitFunction` with the Python API prefixes), which behaves similarly to `PyObject_CallFunction`, in the sense that arguments are generated from a format string.
Note that unlike, `pyawaitable_await`, `pyawaitable_await_function` takes a *callable* object, instead of a coroutine. For example:
```c
static PyObject *
spam(PyObject *self, PyObject *func) // METH_O
{
PyObject *awaitable = pyawaitable_new();
if (awaitable == NULL)
return NULL;
if (pyawaitable_await_function(awaitable, func, "s", NULL, NULL, "hello, world!") < 0)
{
Py_DECREF(awaitable);
return NULL;
}
return awaitable;
}
```

This would be equivalent to the following Python code:

```py
async def func(data: str) -> Any:
...

await func("hello, world!")
```

## Return Values

You can set a return value (the thing that `await c_func()` will evaluate to) via `pyawaitable_set_result` (`PyAwaitable_SetResult` in the Python prefixes). By default, the return value is `None`.
Expand Down

0 comments on commit 8cb78fa

Please sign in to comment.