【问题标题】:How to access mutex created by a C++ process in a Python process?如何在 Python 进程中访问由 C++ 进程创建的互斥锁?
【发布时间】:2019-11-04 23:35:10
【问题描述】:

我计划使用boost::interprocess 在 C++ 和 Python 进程之间共享内存。假设我需要 boost 提供的互斥锁以确保只有一个进程可以访问内存,我如何让 python 确认并解锁/锁定 boost 创建的互斥锁?

【问题讨论】:

    标签: python c++ c++11 boost multiprocessing


    【解决方案1】:

    似乎有两种明显的方法:

    使用上面链接中的示例和一些猜测,你会得到这样的东西:

       static PyObject *mySharedMutex_lock(PyObject *self, PyObject *args)
       {
           const char *objectName;
           int sts;
    
           if (!PyArg_ParseTuple(args, "s", &objectName))
           {
               return NULL;
           }
           boost::interprocess::named_mutex mutex(open_or_create, objectName);
           mutex.lock();
           return Py_None;
       }
    

    显然,您需要上面链接中的说明中的其他样板,并可能提供一种解锁互斥锁的方法。让这个工作看起来不是很繁重。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      相关资源
      最近更新 更多