|
11 | 11 |
|
12 | 12 | ## What is it? |
13 | 13 |
|
14 | | -PyAwaitable is the _only_ library to support writing and calling asynchronous Python functions from pure C code (with the exception of manually implementing an awaitable class from scratch, which is essentially what PyAwaitable does). |
| 14 | +PyAwaitable is the _only_ library to support defining and calling asynchronous Python functions from pure C code. |
15 | 15 |
|
16 | | -It was originally designed to be directly part of CPython--you can read the [scrapped PEP](https://gist.github.com/ZeroIntensity/8d32e94b243529c7e1c27349e972d926) about it. Since this library only uses the public ABI, it's better fit outside of CPython, as a library. |
| 16 | +It was originally designed to be directly part of CPython; you can read the [scrapped PEP](https://gist.github.com/ZeroIntensity/8d32e94b243529c7e1c27349e972d926) about it. But, since this library only uses the public ABI, it's better fit outside of CPython, as a library. |
17 | 17 |
|
18 | 18 | ## Installation |
19 | 19 |
|
@@ -42,18 +42,29 @@ if __name__ == "__main__": |
42 | 42 | ## Example |
43 | 43 |
|
44 | 44 | ```c |
45 | | -#include <pyawaitable.h> |
| 45 | +/* |
| 46 | + Equivalent to the following Python function: |
46 | 47 |
|
47 | | -/* Usage from Python: await my_async_function(coro()) */ |
| 48 | + async def async_function(coro: collections.abc.Awaitable) -> None: |
| 49 | + await coro |
| 50 | +
|
| 51 | + */ |
48 | 52 | static PyObject * |
49 | | -my_async_function(PyObject *self, PyObject *coro) { |
50 | | - /* Make our awaitable object */ |
| 53 | +async_function(PyObject *self, PyObject *coro) |
| 54 | +{ |
| 55 | + // Create our transport between the C world and the asynchronous world. |
51 | 56 | PyObject *awaitable = PyAwaitable_New(); |
| 57 | + if (awaitable == NULL) { |
| 58 | + return NULL; |
| 59 | + } |
52 | 60 |
|
53 | | - /* Mark the coroutine for being awaited */ |
54 | | - PyAwaitable_AddAwait(awaitable, coro, NULL, NULL); |
| 61 | + // Mark our Python coroutine, *coro*, for being executed by the event loop. |
| 62 | + if (PyAwaitable_AddAwait(awaitable, coro, NULL, NULL) < 0) { |
| 63 | + Py_DECREF(awaitable); |
| 64 | + return NULL; |
| 65 | + } |
55 | 66 |
|
56 | | - /* Return the awaitable object to yield to the event loop */ |
| 67 | + // Return our transport, allowing *coro* to be eventually executed. |
57 | 68 | return awaitable; |
58 | 69 | } |
59 | 70 | ``` |
|
0 commit comments