首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌入式Python分段故障

嵌入式Python分段故障
EN

Stack Overflow用户
提问于 2012-01-17 00:16:38
回答 2查看 3K关注 0票数 3

我的多线程应用程序在调用PyImport_ImportModule("my_module")时出现分段错误。

英国电信将张贴在底部。

一些背景:

我的应用程序创建了许多派生grasp_behavior(module)

  • After类的多个实例,并运行基类的Run()函数,该函数使用虚拟方法来确定要做什么。在泛读中,
  1. One派生类使用Python,Grasp_Behavior(类),我使用Python来实现(2) (在下面使用)
  2. ,生成该类的2个实例,并在“并行”(python不真正并行运行)

H 113/code>中运行它们,PyImport_ImportModule节段故障

我的想法是,也许我不能在同一个解释器中两次导入一个模块。但我想不出怎么检查。我想我需要看看grasp_behavior是否在字典中,但我不知道是哪一本,也许我得到了__main__模块的字典?

但是我可能错了,任何建议都是非常有帮助的!

在构造函数中:

代码语言:javascript
运行
复制
//Check if Python is Initialized, otherwise initialize it
if(!Py_IsInitialized())
{
    std::cout << "[GraspBehavior] InitPython: Initializing the Python Interpreter" << std::endl;
    Py_Initialize();
    PyEval_InitThreads(); //Initialize Python thread ability
    PyEval_ReleaseLock(); //Release the implicit lock on the Python GIL
}

// --- Handle Imports ----

PyObject * pModule = PyImport_ImportModule("grasp_behavior");
if(pModule == NULL)
{
    std::cout << "[GraspBehavior] InitPython: Unable to import grasp_behavior module: ";
    PyErr_Print();
}
 // --- Get our Class Pointer From the Module ...
PyObject * pClass = PyObject_GetAttrString(pModule, "Grasp_Behavior");
if(pClass == NULL)
{
    std::cout << "[GraspBehavior] InitPython: Unable to get Class from Module: ";
    PyErr_Print();
}
Py_DECREF(pModule); //clean up, this is a new reference

behavior_instance_ = PyObject_Call(pClass, pArguments_Tuple, pArguments_Dict);
if(behavior_instance_ == NULL)
{
    std::cout << "[GraspBehavior] InitPython: Couldn't generate instance: ";
    PyErr_Print();
}
Py_DECREF(pArguments_Tuple);
Py_DECREF(pArguments_Dict);
Py_DECREF(pClass);

在这里,请注意,我只初始化未初始化的Python解释器。我假设它是为整个过程初始化的。

在Run()方法中(从boost线程运行):

代码语言:javascript
运行
复制
std::cout << "[GraspBehavior] PerformBehavior: Acquiring Python GIL Lock ..." << std::endl;
PyGILState_STATE py_gilstate;
py_gilstate = PyGILState_Ensure();

/* ---- Perform Behavior Below ----- */

std::vector<std::pair<double, double> > desired_body_offsets;
//desired_body_offsets.push_back( std::pair<double, double>(0.6, 0));
PyObject * base_positions = GetTrialBasePositions(my_env_, desired_body_offsets);

PyObject * grasps = EvaluateBasePositions(my_env_, base_positions);

//Did we get any grasps? What do we do with them? [TODO]
if(grasps != NULL)
{
    std::cout << grasps->ob_type->tp_name << std::endl;
    std::cout << "Number of grasps: " << PyList_Size(grasps) << std::endl;
    successful_ = true;
}

/* --------------------------------- */

std::cout << "[GraspBehavior] PerformBehavior: Releasing Python GIL Lock ..." << std::endl;
PyGILState_Release(py_gilstate);

在这里,我和PyGILState锁一起去了。我读了一段时间,似乎许多人都是链接的一些文章使用了Python中较旧的锁定风格.也许我得换一下这个。

回溯:

代码语言:javascript
运行
复制
Program received signal SIGSEGV, Segmentation fault.
0x00007fffee9c4330 in ?? () from /usr/lib/libpython2.6.so.1.0
(gdb) bt
#0  0x00007fffee9c4330 in ?? () from /usr/lib/libpython2.6.so.1.0
#1  0x00007fffee99ff09 in PyEval_GetGlobals ()
   from /usr/lib/libpython2.6.so.1.0
#2  0x00007fffee9bd993 in PyImport_Import () from /usr/lib/libpython2.6.so.1.0
#3  0x00007fffee9bdbec in PyImport_ImportModule ()
   from /usr/lib/libpython2.6.so.1.0
#4  0x000000000042d6f0 in GraspBehavior::InitPython (this=0x7948690)
    at grasp_behavior.cpp:241
EN

Stack Overflow用户

发布于 2012-01-17 00:31:17

Boost是否提供了一个与pthread_once()函数等价的道德功能,允许某些初始化任务精确地运行一次,而不管有多少线程试图同时运行它?如果这是我在调试时遇到的问题,我将尝试保护PyImport_ImportModule不受多个线程中的多个调用的影响,并为此使用一个标准工具,这将是我的第一次尝试。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8888124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档