Skip to content

Commit 8cb78fa

Browse files
committed
Add documentation for pyawaitable_await_function
1 parent 7190b3f commit 8cb78fa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/adding_coros.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ spam(PyObject *self, PyObject *args)
102102
103103
This would be equivalent to `await foo` from Python.
104104
105+
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.
106+
107+
Note that unlike, `pyawaitable_await`, `pyawaitable_await_function` takes a *callable* object, instead of a coroutine. For example:
108+
109+
```c
110+
static PyObject *
111+
spam(PyObject *self, PyObject *func) // METH_O
112+
{
113+
PyObject *awaitable = pyawaitable_new();
114+
115+
if (awaitable == NULL)
116+
return NULL;
117+
118+
if (pyawaitable_await_function(awaitable, func, "s", NULL, NULL, "hello, world!") < 0)
119+
{
120+
Py_DECREF(awaitable);
121+
return NULL;
122+
}
123+
124+
return awaitable;
125+
}
126+
```
127+
128+
This would be equivalent to the following Python code:
129+
130+
```py
131+
async def func(data: str) -> Any:
132+
...
133+
134+
await func("hello, world!")
135+
```
136+
105137
## Return Values
106138

107139
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`.

0 commit comments

Comments
 (0)