저는 파이썬을 C++ 플러그인에 임베드했습니다. 플러그인은 매 세션마다 알고리즘에 다른 데이터를 보낼 때마다 파이썬 알고리즘을 수십 번 호출합니다. 지금까지는 매우 좋은임베디드 파이썬 중지
그러나 문제가 있습니다 : 알고리즘은 해결하기 위해 몇 분이 걸릴 때가 있고 솔루션을 반환하는 경우가 종종 있습니다. 그 동안에는 조건이 변경되어 관련 솔루션과 관련성이 없어집니다. 그래서, 내가 원하는 것은 언제든지 알고리즘의 실행을 멈추고 다른 데이터 세트로 즉시 실행하는 것입니다.
는 여기에 지금까지 가지고 파이썬을 내장하기위한 C++ 코드입니다 :
void py_embed (void*data){
counter_thread=false;
PyObject *pName, *pModule, *pDict, *pFunc;
//To inform the interpreter about paths to Python run-time libraries
Py_SetProgramName(arg->argv[0]);
if(!gil_init){
gil_init=1;
PyEval_InitThreads();
PyEval_SaveThread();
}
PyGILState_STATE gstate = PyGILState_Ensure();
// Build the name object
pName = PyString_FromString(arg->argv[1]);
if(!pName){
textfile3<<"Can't build the object "<<endl;
}
// Load the module object
pModule = PyImport_Import(pName);
if(!pModule){
textfile3<<"Can't import the module "<<endl;
}
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
if(!pDict){
textfile3<<"Can't get the dict"<<endl;
}
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, arg->argv[2]);
if(!pFunc || !PyCallable_Check(pFunc)){
textfile3<<"Can't get the function"<<endl;
}
/*Call the algorithm and treat the data that is returned from it
...
...
*/
// Clean up
Py_XDECREF(pArgs2);
Py_XDECREF(pValue2);
Py_DECREF(pModule);
Py_DECREF(pName);
PyGILState_Release(gstate);
counter_thread=true;
_endthread();
};
편집 : 파이썬의 알고리즘 내 일하지 않고 나는 그것이
이 알고리즘은 실행 작은 단계 (이상적으로 분해 될 수있다 C++ 코드는 다음과 같을 수 있습니다 : 'while (stillNeeded) performNextStep();' –
아니요, 알고리즘은 제 일이 아니므로 변경해서는 안됩니다. –