This project originates from a scrapped PEP. For the original text, see here.
$ pip install pyawaitable
# pyproject.toml example with setuptools
[build-system]
requires = ["setuptools", "pyawaitable"]
build-backend = "setuptools.build_meta"
#include <pyawaitable.h>
// Assuming that this is using METH_O
static PyObject *
hello(PyObject *self, PyObject *coro) {
// Make our awaitable object
PyObject *awaitable = pyawaitable_new();
if (!awaitable)
return NULL;
// Mark the coroutine for being awaited
if (pyawaitable_await(awaitable, coro, NULL, NULL) < 0) {
Py_DECREF(awaitable);
return NULL;
}
// Return the awaitable object to yield to the event loop
return awaitable;
}
# Assuming top-level await
async def coro():
await asyncio.sleep(1)
print("awaited from C!")
# Use our C function to await it
await hello(coro())
pyawaitable
is distributed under the terms of the MIT license.