【问题标题】:Boost python producer consumer problem (execute boost python thread, std::mutex deadlock)boost python生产者消费者问题(执行boost python线程,std::mutex死锁)
【发布时间】: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()

我遇到了两个问题

  1. “wait_thread_function”先启动时,python程序被阻塞
  2. “gen_thread_function”先启动时,显示 Abort 错误信息

所以我的问题是

  1. 我猜第一个问题是由 python GIL 引起的,因为“IntegerQueue”Pop 函数使用 std::mutex 所以我想知道“如何在 Pop 函数中释放 GIL”
  2. 如何从 python 文件中在 boost python 中执行 std::thread?
  3. 是否可以 IntegerQueue 共享 python 文件和 boost python 模块?
  4. 如果队列存储了自定义类类型(不是原始数据类型),它还与 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


    【解决方案1】:

    好的。我找到了解决这些问题的方法..

    1. 我找到了使用作用域 gil 发布的方法。这个question对我很有帮助

    2. 当我运行线程时,我遇到上面的错误窗口......这是因为我无法加入 C++ 模块中的线程。我在运行函数中添加了线程连接代码,错误消失了

    3. 我认为直接共享队列有很多问题..所以我改变了架构。队列对象驻留在 C++ 模块中,并公开访问队列的函数。而且效果很好

    4. 同方案3,我解决暴露C++类的问题

    谢谢

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2011-05-04
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多