解决pybind11分装的函数无法被python多线程处理问题

解决思路:在C++中解开GIL锁

C++封装pybind

#include <glog/logging.h>
#include <thread>
#include <chrono>
#include <iostream>

double SSpeedTime(int i) 
{
    LOG(INFO) << "start...";
    std::chrono::seconds speedtime(2);

    std::this_thread::sleep_for(speedtime);
    LOG(INFO) << "end !";

    return i;
}
double call_SpeedTime(int i) 
{

    py::gil_scoped_release release;
    
    // double a = calculator::Calculator::SpeedTime(i);
    double a = SSpeedTime(i);

    py::gil_scoped_acquire acquire;

    return a;

}

PYBIND11_MODULE(calculator_py, m) {
        m.def("call_SpeedTime", &call_SpeedTime);
}

python调用

import calculator_py
import concurrent.futures
import time
import functools

def worker(i):
    speed = calculator_py.call_SpeedTime(i)
    return speed

# 创建多个线程并启动它们
threads = []
  # 创建线程池
start_T = time.time()

with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
    # 提交任务给线程池
    futures = []
    for i in range(0, 10):
        # 创建偏函数,固定部分参数
        partial_worker = functools.partial(worker, i)
        # 提交任务给线程池
        future = executor.submit(partial_worker)
        futures.append(future)

# 等待所有任务完成
concurrent.futures.wait(futures)

# 获取任务执行结果
end_T = time.time()
print("所有任务完成时间 : " + str((end_T - start_T)*1000) + ", 开始配置结果...")


all = 0
for res in concurrent.futures.as_completed(futures) :
    all = all + res.result()


print(all)
# print("所有任务完成时间 : " + str((end_T - start_T)*1000) + ", 开始配置结果...")

结果输出:

I0726 22:56:20.110416 43808 calculator_py.cpp:28] start...
I0726 22:56:20.111053 43809 calculator_py.cpp:28] start...
I0726 22:56:20.111547 43810 calculator_py.cpp:28] start...
I0726 22:56:20.111605 43811 calculator_py.cpp:28] start...
I0726 22:56:20.112231 43812 calculator_py.cpp:28] start...
I0726 22:56:20.112677 43813 calculator_py.cpp:28] start...
I0726 22:56:20.127169 43814 calculator_py.cpp:28] start...
I0726 22:56:20.128896 43816 calculator_py.cpp:28] start...
I0726 22:56:20.129626 43817 calculator_py.cpp:28] start...
I0726 22:56:20.130002 43818 calculator_py.cpp:28] start...
I0726 22:56:22.110913 43808 calculator_py.cpp:32] end !
I0726 22:56:22.111416 43809 calculator_py.cpp:32] end !
I0726 22:56:22.112051 43810 calculator_py.cpp:32] end !
I0726 22:56:22.112452 43812 calculator_py.cpp:32] end !
I0726 22:56:22.113184 43813 calculator_py.cpp:32] end !
I0726 22:56:22.119035 43811 calculator_py.cpp:32] end !
I0726 22:56:22.127477 43814 calculator_py.cpp:32] end !
I0726 22:56:22.129300 43816 calculator_py.cpp:32] end !
I0726 22:56:22.129884 43817 calculator_py.cpp:32] end !
I0726 22:56:22.130395 43818 calculator_py.cpp:32] end !
所有任务完成时间 : 2022.5729942321777, 开始配置结果...
45.0

成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值