Skip to content

ZeroIntensity/pyawaitable

Repository files navigation

PyAwaitable

CPython API for asynchronous functions

This project originates from a scrapped PEP. For the original text, see here.

Installation

Console

$ pip install pyawaitable

Add it to your project

# pyproject.toml example with setuptools
[build-system]
requires = ["setuptools", "pyawaitable"]
build-backend = "setuptools.build_meta"

Example

#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())

Copyright

pyawaitable is distributed under the terms of the MIT license.

Sponsor this project

 

Contributors 3

  •  
  •  
  •