Call asynchronous code from an extension module.
Project description
PyAwaitable
Call asynchronous code from an extension module
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyawaitable-2.0.1.tar.gz
.
File metadata
- Download URL: pyawaitable-2.0.1.tar.gz
- Upload date:
- Size: 30.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
467a0e554f4a37f8bc061b60571c182516f043094e4be566a363bb582ca95e02
|
|
MD5 |
e3b1b6f08ba4bcef19c7f31faf7f5807
|
|
BLAKE2b-256 |
6129f5bf6bda9ec7e94469e3628d0b935ed4beee7ec3809a7827edcb9546f481
|
File details
Details for the file pyawaitable-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: pyawaitable-2.0.1-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
9080b52da2eadc0c81e24f59ee734c8a037536a72e769931efa26e18b016020d
|
|
MD5 |
c2f92fdbbaeaa0a4b5ea7237079c6d57
|
|
BLAKE2b-256 |
6f5ff261d5323bbac3b160cd5e864f6f721d4a64ed419cfa3b54666ee2c07865
|