From db18b0f8e2a945165af735245acc31f197e6a429 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Wed, 25 Dec 2013 19:26:58 +0800 Subject: [PATCH] fixing embedding Python multithread problems --- PyWinAlfred/Main.cpp | 27 ++++++++++++++++--- WinAlfred.sln | 16 ----------- WinAlfred/MainWindow.xaml | 3 +-- WinAlfred/PluginLoader/PythonPluginLoader.cs | 9 ++++--- WinAlfred/PluginLoader/PythonPluginWrapper.cs | 4 ++- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/PyWinAlfred/Main.cpp b/PyWinAlfred/Main.cpp index 4ff589a1ea..4e923cb76c 100644 --- a/PyWinAlfred/Main.cpp +++ b/PyWinAlfred/Main.cpp @@ -1,14 +1,30 @@ #include #include "Python.h" + +extern "C" __declspec(dllexport) void InitPythonEnv() +{ + +} + extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* query) { try{ - PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance ; + PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance; - // Initialize the Python Interpreter + // Initialise the Python interpreter Py_Initialize(); + // Create GIL/enable threads + PyEval_InitThreads(); + + 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 PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PyObject_GetAttrString(sys, "path"); @@ -37,10 +53,13 @@ extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, c // Call a method of the class with two parameters pValue = PyObject_CallMethod(pInstance,"query", "(s)",query); - char * str_ret = PyString_AsString(pValue); + char * str_ret = PyString_AsString(pValue); + + PyGILState_Release(gstate); + //PyEval_SaveThread(); // Finish the Python Interpreter - Py_Finalize(); + //Py_Finalize(); return str_ret; } diff --git a/WinAlfred.sln b/WinAlfred.sln index a478b6711d..1b27e33db8 100644 --- a/WinAlfred.sln +++ b/WinAlfred.sln @@ -7,8 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin", "WinAlfr EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugin", "Plugin", "{3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin.Everything", "Plugins\WinAlfred.Plugin.Everything\WinAlfred.Plugin.Everything.csproj", "{230AE83F-E92E-4E69-8355-426B305DA9C0}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin.System", "Plugins\WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj", "{E515011D-A769-418B-8761-ABE6F29827A0}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred", "WinAlfred\WinAlfred.csproj", "{DB90F671-D861-46BB-93A3-F1304F5BA1C5}" @@ -60,19 +58,6 @@ Global {8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|Win32.Build.0 = Release|x86 {8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x64.ActiveCfg = Release|Any CPU {8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x86.ActiveCfg = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Any CPU.ActiveCfg = Debug|x64 - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Any CPU.Build.0 = Debug|x64 - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|Win32.ActiveCfg = Debug|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|x64.ActiveCfg = Debug|x64 - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|x86.ActiveCfg = Debug|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Debug|x86.Build.0 = Debug|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Any CPU.Build.0 = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|Win32.ActiveCfg = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|x64.ActiveCfg = Release|Any CPU - {230AE83F-E92E-4E69-8355-426B305DA9C0}.Release|x86.ActiveCfg = Release|Any CPU {E515011D-A769-418B-8761-ABE6F29827A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E515011D-A769-418B-8761-ABE6F29827A0}.Debug|Any CPU.Build.0 = Debug|Any CPU {E515011D-A769-418B-8761-ABE6F29827A0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU @@ -124,7 +109,6 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {230AE83F-E92E-4E69-8355-426B305DA9C0} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87} {E515011D-A769-418B-8761-ABE6F29827A0} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87} EndGlobalSection EndGlobal diff --git a/WinAlfred/MainWindow.xaml b/WinAlfred/MainWindow.xaml index 3713cbc112..0d1d3d8bbd 100644 --- a/WinAlfred/MainWindow.xaml +++ b/WinAlfred/MainWindow.xaml @@ -1,7 +1,6 @@  LoadPlugin() { List plugins = new List(); @@ -21,9 +21,12 @@ namespace WinAlfred.PluginLoader }; plugins.Add(pair); } + + foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin)) + { + new Thread(plugin.Init).Start(); + } return plugins; } - - } } diff --git a/WinAlfred/PluginLoader/PythonPluginWrapper.cs b/WinAlfred/PluginLoader/PythonPluginWrapper.cs index 094b0a4a9a..4040476e45 100644 --- a/WinAlfred/PluginLoader/PythonPluginWrapper.cs +++ b/WinAlfred/PluginLoader/PythonPluginWrapper.cs @@ -12,6 +12,8 @@ namespace WinAlfred.PluginLoader [DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private extern static IntPtr ExecPython(string directory, string file, string query); + [DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private extern static void InitPythonEnv(); public PythonPluginWrapper(PluginMetadata metadata) { @@ -27,7 +29,7 @@ namespace WinAlfred.PluginLoader public void Init() { - + InitPythonEnv(); } } }