Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
第二次运行的线程出现错误 < >我使用Windows中的线程将C++中的插件连接到Python脚本。在会话期间,该线程将被多次调用_Python_C++_Multithreading_Visual Studio 2012 - Fatal编程技术网

第二次运行的线程出现错误 < >我使用Windows中的线程将C++中的插件连接到Python脚本。在会话期间,该线程将被多次调用

第二次运行的线程出现错误 < >我使用Windows中的线程将C++中的插件连接到Python脚本。在会话期间,该线程将被多次调用,python,c++,multithreading,visual-studio-2012,Python,C++,Multithreading,Visual Studio 2012,问题: 第二次调用调用python脚本的工作线程时,会发生以下情况: ->.exe程序崩溃,代码为: 运行时错误! 程序:xxx.exe。 此应用程序已请求运行时以异常方式终止它。有关更多信息,请联系应用程序的支持团队 :在XXX.EXE中0x735C41F的第一个机会异常:微软C++异常:CMASIDIAGART异常在内存位置0x06A9F840。 如果存在此异常的处理程序,则程序可以安全地继续 将我引向mlock.c的以下几行: 我在哪里没有正确处理异常 在调试过程中,我注意到最后一行的_e

问题:

第二次调用调用python脚本的工作线程时,会发生以下情况:

->.exe程序崩溃,代码为: 运行时错误! 程序:xxx.exe。 此应用程序已请求运行时以异常方式终止它。有关更多信息,请联系应用程序的支持团队

:在XXX.EXE中0x735C41F的第一个机会异常:微软C++异常:CMASIDIAGART异常在内存位置0x06A9F840。 如果存在此异常的处理程序,则程序可以安全地继续

将我引向mlock.c的以下几行:

我在哪里没有正确处理异常

在调试过程中,我注意到最后一行的_endthread从未到达,我也不知道为什么。这是问题的根源吗

代码:

以下是工作线程的代码:

void py_embed (void*data){

char *argv[4]={"PythonPlugIn2","bridge","test_callsign","MAH543"};
int argc=4;

ofstream textfile3;
textfile3.open("FP_python_embed.txt");

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
PyThreadState *mainThreadState,*myThreadState,*tempState;
PyInterpreterState *mainInterpreterState;

//To inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(argv[0]);

// Initialize the Python Interpreter
Py_Initialize();

// Initialize thread support
PyEval_InitThreads();

// Save a pointer to the main PyThreadState object
mainThreadState = PyThreadState_Get();

// Get a reference to the PyInterpreterState
mainInterpreterState = mainThreadState->interp;

// Create a thread state object for this thread
myThreadState = PyThreadState_New(mainInterpreterState);

// Release global lock
PyEval_ReleaseLock();

// Acquire global lock
PyEval_AcquireLock();

// Swap in my thread state
tempState = PyThreadState_Swap(myThreadState);

// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, argv[2]);

//Do the Python things
PyObject *pArgs2, *pValue2;
pArgs2=Py_BuildValue("(s)",argv[3]);
pValue2 = PyObject_CallObject(pFunc, pArgs2);
textfile3<<PyInt_AsLong(pValue2)<<endl<<" worked1";
textfile3.close();

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Swap out the current thread
PyThreadState_Swap(tempState);

// Release global lock
PyEval_ReleaseLock();

// Clean up thread state
PyThreadState_Clear(myThreadState);
PyThreadState_Delete(myThreadState);

// Finish the Python Interpreter
Py_Finalize();

_endthread();
};

注:与此相关的问题2是。

请每次提问一个问题。您认为我应该将此问题一分为二吗?您自己已经将其一分为二了。我也面临着这个问题。第二次用Py_Initialize调用函数会出现错误,你能告诉我解决方案吗?我不能,因为这是多年前的事了,我不记得了。抱歉@AreebMuzaffar
void py_embed (void*data){

char *argv[4]={"PythonPlugIn2","bridge","test_callsign","MAH543"};
int argc=4;

ofstream textfile3;
textfile3.open("FP_python_embed.txt");

PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
PyThreadState *mainThreadState,*myThreadState,*tempState;
PyInterpreterState *mainInterpreterState;

//To inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(argv[0]);

// Initialize the Python Interpreter
Py_Initialize();

// Initialize thread support
PyEval_InitThreads();

// Save a pointer to the main PyThreadState object
mainThreadState = PyThreadState_Get();

// Get a reference to the PyInterpreterState
mainInterpreterState = mainThreadState->interp;

// Create a thread state object for this thread
myThreadState = PyThreadState_New(mainInterpreterState);

// Release global lock
PyEval_ReleaseLock();

// Acquire global lock
PyEval_AcquireLock();

// Swap in my thread state
tempState = PyThreadState_Swap(myThreadState);

// Build the name object
pName = PyString_FromString(argv[1]);

// Load the module object
pModule = PyImport_Import(pName);

// pDict is a borrowed reference 
pDict = PyModule_GetDict(pModule);

// pFunc is also a borrowed reference 
pFunc = PyDict_GetItemString(pDict, argv[2]);

//Do the Python things
PyObject *pArgs2, *pValue2;
pArgs2=Py_BuildValue("(s)",argv[3]);
pValue2 = PyObject_CallObject(pFunc, pArgs2);
textfile3<<PyInt_AsLong(pValue2)<<endl<<" worked1";
textfile3.close();

// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);

// Swap out the current thread
PyThreadState_Swap(tempState);

// Release global lock
PyEval_ReleaseLock();

// Clean up thread state
PyThreadState_Clear(myThreadState);
PyThreadState_Delete(myThreadState);

// Finish the Python Interpreter
Py_Finalize();

_endthread();
};
handle=(HANDLE) _beginthread(py_embed,0,NULL);