python plugin test

This commit is contained in:
qianlifeng
2014-01-03 19:29:46 +08:00
parent 188e7910dd
commit 411932c96b
3 changed files with 35 additions and 50 deletions

View File

@@ -7,9 +7,10 @@ int i = 0;
extern "C" __declspec(dllexport) void InitPythonEnv()
{
i++;
Py_Initialize();
PyEval_InitThreads();
PyEval_ReleaseLock();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ǰִ<C7B0>У<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ͷ<EFBFBD>PyEval_InitThreads<64><73><EFBFBD>õ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳̿<DFB3><CCBF><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD>ȡ<EFBFBD><C8A1>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
char* GetErrorMessage()
@@ -30,25 +31,11 @@ char* Exec(char* directory, char* file, char* method, char* para)
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance;
char *error;
i++;
PyThreadState* global_state = PyThreadState_Get();
PyThreadState* ts = Py_NewInterpreter();
PyThreadState_Swap(ts);
// Initialise the Python interpreter
// Create GIL/enable threads
//PyGILState_STATE gstate = PyGILState_Ensure();
// // Get the default thread state
// PyThreadState* state = PyThreadState_Get();
// // Once in each thread
//PyThreadState* stateForNewThread = PyThreadState_New(state->interp);
//PyEval_RestoreThread(stateForNewThread);
PyGILState_STATE gstate = PyGILState_Ensure();
// Build the name object
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyObject *path = PySys_GetObject("path");
PyList_Append(path, PyString_FromString(directory));
pName = PyString_FromString(file);
@@ -102,19 +89,17 @@ char* Exec(char* directory, char* file, char* method, char* para)
char * str_ret = PyString_AsString(pValue);
//PyEval_SaveThread();
// Finish the Python Interpreter
PyErr_Clear();
Py_EndInterpreter(ts);
PyThreadState_Swap(global_state);
//PyErr_Clear();
PyGILState_Release(gstate);
return str_ret;
}
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* method, char* para)
{
auto future = std::async(Exec,directory,file,method,para);
return future.get();
char* s = Exec(directory,file,method,para);
PyGILState_Ensure();
return s;
//auto future = std::async(Exec,directory,file,method,para);
//return future.get();
}