Man*_*d3r 6 c++ python multithreading pybind11
我想在工作线程中运行 Python。但是,我在工作池中遇到了奇怪的段错误和线程死锁。如何正确使用 pybind11/Python C API 来允许线程运行作业?
我知道由于 GIL,它对 MT python 没有多大意义,但这是适应当前架构的中间解决方案,直到有更好的方法。
小智 6
这有效。用 gil_scoped_release 和 gil_scoped_acquire 包装长时间运行的 c++ 代码
pybind11::gil_scoped_release release;
while (true)
{
// do something and break
}
pybind11::gil_scoped_acquire acquire;
Run Code Online (Sandbox Code Playgroud)
如果您想要执行以下操作 - 在 Python 中您想要运行 C++ 线程以在每个线程上执行不同的任务。
创建一个 theradpool,如https://github.com/progschj/ThreadPool
使用 PyBind11 编写一个包装器类来包装并绑定到 Python。创建 ThreadPool 实例并从 Python 添加任务,Python 又创建不同的线程来执行任务。
免责声明 - 我没有尝试过,但这会起作用:)
| 归档时间: |
|
| 查看次数: |
3557 次 |
| 最近记录: |