fixing embedding Python multithread problems

This commit is contained in:
qianlifeng 2013-12-25 19:26:58 +08:00
parent 94cd2c0599
commit db18b0f8e2
5 changed files with 33 additions and 26 deletions

View File

@ -1,14 +1,30 @@
#include <tchar.h>
#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;
}

View File

@ -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

View File

@ -1,7 +1,6 @@
<Window x:Class="WinAlfred.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winAlfred="clr-namespace:WinAlfred"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:winAlfred="clr-namespace:WinAlfred"
Title="WinAlfred" Height="80" Width="525"
Background="#ebebeb"
Topmost="True"

View File

@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using WinAlfred.Plugin;
namespace WinAlfred.PluginLoader
{
public class PythonPluginLoader : BasePluginLoader
{
public override List<PluginPair> LoadPlugin()
{
List<PluginPair> plugins = new List<PluginPair>();
@ -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;
}
}
}

View File

@ -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();
}
}
}