ywjb / asynchronizer

Python asyncio <-> Boost.Asio bridge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python asyncio <-> Boost.Asio bridge


What is this?

asynchronizer is a simple bridge for synchronization between Boost.Asio and Python 3 asyncio. It provides small Python extension (using pybind11) and Python wrapper.

What is it for?

  • You write complex application with asynchronous C++ code and want to use some Python to simplify this challenge.
  • You have awesome C++ library using Boost.Asio and want create Python bindings for it.

License

MIT license. See license.txt.

Requirements

  • Compiler with C++14 support. For example, try g++ of versions 5/6/... or modern clang.
  • Boost (or just Boost.Asio and Boost.System) (>= 1.66.0 because of Boost.Asio refactoring since 1.65)
    • Tested: Boost 1.68.0 on Mac OS X 10.11
  • Boost.Build (or any other build system -- it's simple).
    • Note: Set environment variable BOOST_ROOT to unpacked Boost directory path.
  • Python >=3.6
    • Tested: Python 3.6.4 on Mac OS X 10.11
  • Some Python packages (usually installed by pip; see Installation section and files: requirements.txt, testing-requirements.txt).

Installation

Note: Using Python venv is recommended.

  1. Install Python dependencies:
pip install -r requirements.txt

Note: Install dependencies for testing, if you want run tests:

pip install -r testing-requirements.txt
  1. Build Python extension and stub:
b2
  1. Install asynchronizer.py, Python extension and stub for it into current site-packages directory:
b2 install
  1. Run some tests, if you want:
b2 install-test-async-sleep-ext
PYTHONPATH="$( pwd )/build/ext:$PYTHONPATH" pytest

Using in your project

Just write your modules using pybind11 as usual!

Wrap your asynchronous code like ext_src/test_async_sleep_ext.cpp does.

Note: asynchronizer (with some pybind11 magic) provides bindings for some Boost.Asio, so it's enough to import IoContext and other from installed asynchronizer module in your Python code: you are not required to copy IoContext wrapper bindings into your project.

Optional: If your code require std::shared_ptr-managed classes, you can:

  1. use directory include in header search path in your project
  2. do this in your project:
    #include <dkuk/asynchronizer/asynchronizer.hpp>
    
    PYBIND11_MODULE(my_module, module)
    {
        ...
        dkuk::asynchronizer::shared_ptr_managed_class<my_class>{module, "MyClass"};
        ...
    }
    

Examples

See tests.


Author: Dmitry Kukovinets, d1021976@gmail.com, 16.03.2019 10:18

About

Python asyncio <-> Boost.Asio bridge

License:MIT License


Languages

Language:Python 56.1%Language:C++ 43.9%