如何使用带有boost.python的asyncio? [英] how to use asyncio with boost.python?
问题描述
是否可以将Python3 asyncio
软件包与Boost.Python
库一起使用?
Is it possible to use Python3 asyncio
package with Boost.Python
library?
我有使用Boost.Python
构建的CPython
C++
扩展名.用C++
编写的函数可以工作很长时间.我想使用asyncio
调用这些函数,但是res = await cpp_function()
代码不起作用.
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.
- 在协程内部调用
cpp_function
会发生什么? - 如何不调用工作时间很长的
C++
函数来阻止它?
- What happens when
cpp_function
is called inside coroutine? - How not get blocked by calling
C++
function that works very long time?
注意:C++
不执行某些I/O操作,只是进行计算.
NOTE: C++
doesn't do some I/O operations, just calculations.
推荐答案
在协程内部调用cpp_function会发生什么?
What happens when cpp_function is called inside coroutine?
如果您在任何协程中调用长时间运行的Python/C函数,它将冻结事件循环(冻结所有协程的各处).
If you call long-running Python/C function inside any of your coroutines, it freezes your event loop (freezes all coroutines everywhere).
您应该避免这种情况.
如何通过调用工作时间很长的C ++函数不会被阻塞
How not get blocked by calling C++ function that works very long time
您应该使用 run_in_executor 来在其中运行功能单独的线程或进程. run_in_executor
返回您可以等待的协程.
You should use run_in_executor to run you function in separate thread or process. run_in_executor
returns coroutine that you can await.
由于GIL,您可能需要ProcessPoolExecutor
(我不确定您是否选择ThreadPoolExecutor
,但我建议您检查一下).
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()
这篇关于如何使用带有boost.python的asyncio?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!