【发布时间】:2021-06-13 09:07:26
【问题描述】:
我正在尝试创建 boost python 模块并测试生产者和消费者问题。
但是我遇到了一些错误和问题......
下图显示了我的目标架构。
我实现了 boost python 模块
这里是python代码sn-p (main.py)
import threading
import datetime
import PyDataTest
queue = None
def thread_function():
print('[thread_function] start...')
while True:
print('[thread_function] try Pop')
data = queue.Pop()
print(f'[thread_function] Pop: {data}')
def wait_thread_function():
print('[wait_thread_function] start...')
wait_thread = threading.Thread(target=thread_function)
wait_thread.start()
wait_thread.join()
def gen_thread_function():
gen_thread_obj = PyDataTest.CustomThread()
gen_thread = threading.Thread(target=gen_thread_obj.Execute)
gen_thread.start()
gen_thread.join()
if __name__ == '__main__':
print('main start')
queue = PyDataTest.GetQueueInstance()
wait_thread_func = threading.Thread(target=wait_thread_function, args=())
wait_thread_func.start()
gen_thread_func = threading.Thread(target=gen_thread_function)
gen_thread_func.start()
timeVal = datetime.datetime.now()
while True:
currentTime = datetime.datetime.now()
if (currentTime - timeVal).seconds > 10:
print('Python Main running ...')
thread.join()
gen_thread.join()
我遇到了两个问题
- “wait_thread_function”先启动时,python程序被阻塞
- “gen_thread_function”先启动时,显示 Abort 错误信息
所以我的问题是
- 我猜第一个问题是由 python GIL 引起的,因为“IntegerQueue”Pop 函数使用 std::mutex 所以我想知道“如何在 Pop 函数中释放 GIL”
- 如何从 python 文件中在 boost python 中执行 std::thread?
- 是否可以 IntegerQueue 共享 python 文件和 boost python 模块?
- 如果队列存储了自定义类类型(不是原始数据类型),它还与 python 文件和 boost python 模块共享?
我的最终目标是基于生产者和消费者模型以及通过队列对象共享的自定义对象类型数据
请帮帮我
谢谢
环境
- python:3.9.2 (AMD64)
- C++:Visual Studio 2019
- 提升:1.75.0
其他信息
弹出功能码sn-p
bool Pop(T& obj)
{
std::unique_lock<std::shared_mutex> ul(sm);
cv.wait(ul, [this] {return (!con.empty() || !isRun); });
if (!isRun)
{
return false;
}
if (!con.empty())
{
obj = con.front();
con.pop();
return true;
}
return false;
}
================================================ ==========================
“gen_thread_function”首先运行时的错误消息
【问题讨论】:
标签: python c++ boost python-multithreading boost-python