Skip to main content

PyAwaitable

Call asynchronous code from an extension module

Build Tests

What is it?

PyAwaitable is the only library to support defining and calling asynchronous Python functions from pure C code.

It was originally designed to be directly part of CPython; you can read the scrapped PEP about it. But, since this library only uses the public ABI, it's better fit outside of CPython, as a library.

Installation

Add it to your project's build process:

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

Include it in your extension:

from setuptools import setup, Extension
import pyawaitable

if __name__ == "__main__":
    setup(
        ...,
        ext_modules=[Extension(..., include_dirs=[pyawaitable.include()])]
    )

Example

/*
 Equivalent to the following Python function:

 async def async_function(coro: collections.abc.Awaitable) -> None:
    await coro

 */
static PyObject *
async_function(PyObject *self, PyObject *coro)
{
    // Create our transport between the C world and the asynchronous world.
    PyObject *awaitable = PyAwaitable_New();
    if (awaitable == NULL) {
        return NULL;
    }

    // Mark our Python coroutine, *coro*, for being executed by the event loop.
    if (PyAwaitable_AddAwait(awaitable, coro, NULL, NULL) < 0) {
        Py_DECREF(awaitable);
        return NULL;
    }

    // Return our transport, allowing *coro* to be eventually executed.
    return awaitable;
}

Copyright

pyawaitable is distributed under the terms of the MIT license.

Metadata

Release files for pyawaitable 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyawaitable 2.0.1
File Size Uploaded
pyawaitable-2.0.1.tar.gz 30.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyawaitable 2.0.1
File Interpreter ABI Platform
pyawaitable-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 46.6 kB

Release files / pyawaitable-2.0.1.tar.gz

Download URL pyawaitable-2.0.1.tar.gz
Size 30.0 kB
Tags Source
SHA-256 checksum
How to use checksums
467a0e554f4a37f8bc061b60571c182516f043094e4be566a363bb582ca95e02
BLAKE2b-256 checksum
How to use checksums
6129f5bf6bda9ec7e94469e3628d0b935ed4beee7ec3809a7827edcb9546f481
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8

Release files / pyawaitable-2.0.1-py3-none-any.whl

Download URL pyawaitable-2.0.1-py3-none-any.whl
Size 16.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9080b52da2eadc0c81e24f59ee734c8a037536a72e769931efa26e18b016020d
BLAKE2b-256 checksum
How to use checksums
6f5ff261d5323bbac3b160cd5e864f6f721d4a64ed419cfa3b54666ee2c07865
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.8
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page