https://github.com/toberocat/pybind11-asyncio-coroutine-example
This repository demonstrates a basic integration of C++ coroutines with Python's asyncio using Pybind11. It provides a minimal example of exposing C++ iterators as Python asynchronous iterators, enabling asyncio compatibility without threading.
https://github.com/toberocat/pybind11-asyncio-coroutine-example
asyncio coroutines cpp pybind11 python3
Last synced: 3 months ago
JSON representation
This repository demonstrates a basic integration of C++ coroutines with Python's asyncio using Pybind11. It provides a minimal example of exposing C++ iterators as Python asynchronous iterators, enabling asyncio compatibility without threading.
- Host: GitHub
- URL: https://github.com/toberocat/pybind11-asyncio-coroutine-example
- Owner: ToberoCat
- License: other
- Created: 2025-01-19T10:07:08.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-29T16:53:23.000Z (over 1 year ago)
- Last Synced: 2025-06-24T14:51:42.567Z (about 1 year ago)
- Topics: asyncio, coroutines, cpp, pybind11, python3
- Language: C++
- Homepage:
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π Asyncio Coroutine Integration with Pybind11
[](https://github.com/)
## π Overview
This repository provides a minimal yet functional demonstration of integrating **C++20 coroutines** with Python's *
*asyncio** using **Pybind11**. The goal is to allow calling C++ coroutine functions directly and using them with `await`
in Python.
**Why this project?**
Despite extensive research, no clean and simple way to achieve this was found. This repository serves as a starting
point for those looking to bridge C++ coroutines with Pythonβs asyncio.
## π Project Structure
```
π¦ asyncio-coroutine-pybind11
βββ src/
β βββ main.cpp # C++20 coroutine logic with Pybind11 bindings
βββ example/
β βββ main.py # Python example showcasing async iterator usage
βββ install.sh # Script to install dependencies and build extension
βββ README.md # Documentation
```
## π Features
β
**Bridges C++ coroutines with Python asyncio**
β
**No threadingβpure coroutine-based execution**
β
**Provides an async iterator directly callable in Python**
β
**Simple, minimal, and easily extendable**
### π Example
```python
import asyncio
from asyncio_example import start_counter
async def main():
task1 = start_counter(10)
task2 = start_counter(15)
await task1
await task2
asyncio.run(main())
```
This example demonstrates two concurrent counter coroutines running asynchronously.
## π οΈ Prerequisites
Ensure you have the following installed:
- Python 3.7+ (with `venv` support)
- A **C++17+ compiler** (e.g., `g++`, `clang`)
- **CMake** (for building C++ extension)
- **Ninja** (optional, for faster builds)
- **Pybind11** (installed automatically)
## π₯ Clone the Repository
```bash
git clone https://github.com/ToberoCat/pybind11-asyncio-coroutine-example.git --recursive
cd pybind11-asyncio-coroutine-example
```
## β‘ Installation & Usage
### πΉ Step 1: Set up a Virtual Environment
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
### πΉ Step 2: Install & Build the Extension
Run the provided install script:
```bash
./install.sh
```
### πΉ Step 3: Run the Example
```bash
python example/main.py
```
## π Expected Output
Running `main.py` will create two concurrent counter coroutines. The expected output should resemble:
```
Yielding 0
Yielding 1
Yielding 2
...
Yielding 10
Yielding 15
```
It demonstrates **asynchronous iteration** where Python's `asyncio` can await a C++ coroutine without blocking
execution.
## β οΈ Limitations
π§ **This is a Proof-of-Concept (PoC).**
- Not optimized for production.
- Error handling and resource management can be improved.
- Further refinements and enhancements are encouraged.
## π€ Contributing
Contributions are welcome! If you find a bug, have suggestions, or want to enhance functionality, feel free to:
1. Fork this repository
2. Create a feature branch (`git checkout -b feature-name`)
3. Commit your changes (`git commit -m "Add new feature"`)
4. Push to your branch (`git push origin feature-name`)
5. Open a **Pull Request**
## π Resources
- [Pybind11 Documentation](https://pybind11.readthedocs.io/en/stable/)
- [C++ Coroutines](https://en.cppreference.com/w/cpp/coroutine)
- [Asyncio in Python](https://docs.python.org/3/library/asyncio.html)
This repository serves as a **starting point** for integrating **C++20 coroutines** with **Python asyncio** using *
*Pybind11**. Feel free to explore, modify, and improve it as needed! π