how to use asyncio with boost.python?

我只是一个虾纸丫 提交于 2019-12-12 15:37:59

问题


Is it possible to use Python3 asyncio package with Boost.Python library?

I have CPython C++ extension that builds with Boost.Python. And functions that are written in C++ can work really long time. I want to use asyncio to call these functions but res = await cpp_function() code doesn't work.

  • What happens when cpp_function is called inside coroutine?
  • How not get blocked by calling C++ function that works very long time?

NOTE: C++ doesn't do some I/O operations, just calculations.


回答1:


What happens when cpp_function is called inside coroutine?

If you call long-running Python/C function inside any of your coroutines, it freezes your event loop (freezes all coroutines everywhere).

You should avoid this situation.

How not get blocked by calling C++ function that works very long time

You should use run_in_executor to run you function in separate thread or process. run_in_executor returns coroutine that you can await.

You'll probably need ProcessPoolExecutor because of GIL (I'm not sure if ThreadPoolExecutor is option in your situation, but I advice you to check it).

Here's example of awaiting long-running code:

import asyncio
from concurrent.futures import ProcessPoolExecutor
import time


def blocking_function():
    # Function with long-running C/Python code.
    time.sleep(3)
    return True


async def main():        
    # Await of executing in other process,
    # it doesn't block your event loop:
    loop = asyncio.get_event_loop()
    res = await loop.run_in_executor(executor, blocking_function)


if __name__ == '__main__':
    executor = ProcessPoolExecutor(max_workers=1)  # Prepare your executor somewhere.

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        loop.run_until_complete(main())
    finally:
        loop.run_until_complete(loop.shutdown_asyncgens())
        loop.close()


来源:https://stackoverflow.com/questions/44488218/how-to-use-asyncio-with-boost-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!