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

@@ -23,19 +23,10 @@ char* Exec(char* directory, char* file, char* method, char* para)
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance; PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance;
char *error; char *error;
//PyThreadState* global_state = PyThreadState_Get(); PyGILState_STATE gstate = PyGILState_Ensure();
//PyThreadState* ts = Py_NewInterpreter();
//PyThreadState_Swap(ts);
// Initialise the Python interpreter // 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);
// Build the name object // Build the name object
PyObject *sys = PyImport_ImportModule("sys"); PyObject *sys = PyImport_ImportModule("sys");
@@ -98,27 +89,36 @@ char* Exec(char* directory, char* file, char* method, char* para)
// Finish the Python Interpreter // Finish the Python Interpreter
//PyErr_Clear(); //PyErr_Clear();
//Py_EndInterpreter(ts);
//PyThreadState_Swap(global_state);
printf("My thread is finishing... %s \n",para);
PyGILState_Release(gstate); PyGILState_Release(gstate);
return str_ret; return str_ret;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char* directory = "d:\\Personal\\WinAlfred\\WinAlfred\\bin\\Debug\\Plugins\\p4pperson\\"; char* directory = "d:\\github\\WinAlfred\\WinAlfred\\bin\\Debug\\Plugins\\p";
char* file = "main"; char* file = "main";
char* method = "query"; char* method = "query";
char* para = "p q"; char* para1 = "p 1";
char* para2 = "p 2";
char* para3 = "p 3";
char* para4 = "p 4";
int i = 0;
// <20><>ʼ<EFBFBD><CABC>
Py_Initialize(); Py_Initialize();
// <20><>ʼ<EFBFBD><CABC><EFBFBD>߳<EFBFBD>֧<EFBFBD><D6A7>
PyEval_InitThreads(); PyEval_InitThreads();
for(int i=0;i++;i<10){ PyEval_ReleaseLock();
auto future = std::async(Exec,directory,file,method,para); // <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>
printf("%s",future.get()); //std::async(Exec,directory,file,method,para);
} std::async(Exec,directory,file,method,para1);
Py_Finalize(); std::async(Exec,directory,file,method,para2);
std::async(Exec,directory,file,method,para3);
std::async(Exec,directory,file,method,para4);
// <20><>֤<EFBFBD><D6A4><EFBFBD>̵߳<DFB3><CCB5>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//PyGILState_Ensure();
getchar(); getchar();
Py_Finalize();
return 0; return 0;
} }

View File

@@ -7,9 +7,10 @@ int i = 0;
extern "C" __declspec(dllexport) void InitPythonEnv() extern "C" __declspec(dllexport) void InitPythonEnv()
{ {
i++;
Py_Initialize(); Py_Initialize();
PyEval_InitThreads(); 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() char* GetErrorMessage()
@@ -30,25 +31,11 @@ char* Exec(char* directory, char* file, char* method, char* para)
{ {
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance; PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance;
char *error; char *error;
i++;
PyThreadState* global_state = PyThreadState_Get(); PyGILState_STATE gstate = PyGILState_Ensure();
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);
// Build the name object // Build the name object
PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PySys_GetObject("path");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString(directory)); PyList_Append(path, PyString_FromString(directory));
pName = PyString_FromString(file); pName = PyString_FromString(file);
@@ -102,19 +89,17 @@ char* Exec(char* directory, char* file, char* method, char* para)
char * str_ret = PyString_AsString(pValue); char * str_ret = PyString_AsString(pValue);
//PyEval_SaveThread(); //PyErr_Clear();
PyGILState_Release(gstate);
// Finish the Python Interpreter
PyErr_Clear();
Py_EndInterpreter(ts);
PyThreadState_Swap(global_state);
return str_ret; return str_ret;
} }
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* method, char* para) extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* method, char* para)
{ {
auto future = std::async(Exec,directory,file,method,para); char* s = Exec(directory,file,method,para);
return future.get(); PyGILState_Ensure();
return s;
//auto future = std::async(Exec,directory,file,method,para);
//return future.get();
} }

View File

@@ -68,8 +68,8 @@ namespace WinAlfred
private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e) private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{ {
string query = tbQuery.Text; string query = tbQuery.Text;
//ThreadPool.QueueUserWorkItem(state => ThreadPool.QueueUserWorkItem(state =>
//{ {
results.Clear(); results.Clear();
foreach (PluginPair pair in plugins) foreach (PluginPair pair in plugins)
{ {
@@ -98,7 +98,7 @@ namespace WinAlfred
resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList()); resultCtrl.AddResults(results.OrderByDescending(o => o.Score).ToList());
resultCtrl.SelectFirst(); resultCtrl.SelectFirst();
})); }));
//}); });
} }
private void HideWinAlfred() private void HideWinAlfred()