mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 22:43:31 +08:00
fixing embedding Python multithread problems
This commit is contained in:
parent
94cd2c0599
commit
db18b0f8e2
@ -1,14 +1,30 @@
|
|||||||
#include <tchar.h>
|
#include <tchar.h>
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern "C" __declspec(dllexport) void InitPythonEnv()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* query)
|
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* query)
|
||||||
{
|
{
|
||||||
try{
|
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();
|
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
|
// Build the name object
|
||||||
PyObject *sys = PyImport_ImportModule("sys");
|
PyObject *sys = PyImport_ImportModule("sys");
|
||||||
PyObject *path = PyObject_GetAttrString(sys, "path");
|
PyObject *path = PyObject_GetAttrString(sys, "path");
|
||||||
@ -39,8 +55,11 @@ extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, c
|
|||||||
pValue = PyObject_CallMethod(pInstance,"query", "(s)",query);
|
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
|
// Finish the Python Interpreter
|
||||||
Py_Finalize();
|
//Py_Finalize();
|
||||||
|
|
||||||
return str_ret;
|
return str_ret;
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin", "WinAlfr
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugin", "Plugin", "{3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugin", "Plugin", "{3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}"
|
||||||
EndProject
|
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}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred.Plugin.System", "Plugins\WinAlfred.Plugin.System\WinAlfred.Plugin.System.csproj", "{E515011D-A769-418B-8761-ABE6F29827A0}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAlfred", "WinAlfred\WinAlfred.csproj", "{DB90F671-D861-46BB-93A3-F1304F5BA1C5}"
|
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|Win32.Build.0 = Release|x86
|
||||||
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x64.ActiveCfg = Release|Any CPU
|
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}.Release|x86.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.ActiveCfg = Debug|Any CPU
|
||||||
{E515011D-A769-418B-8761-ABE6F29827A0}.Debug|Any CPU.Build.0 = 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
|
{E515011D-A769-418B-8761-ABE6F29827A0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
|
||||||
@ -124,7 +109,6 @@ Global
|
|||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(NestedProjects) = preSolution
|
GlobalSection(NestedProjects) = preSolution
|
||||||
{230AE83F-E92E-4E69-8355-426B305DA9C0} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
|
|
||||||
{E515011D-A769-418B-8761-ABE6F29827A0} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
|
{E515011D-A769-418B-8761-ABE6F29827A0} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<Window x:Class="WinAlfred.MainWindow"
|
<Window x:Class="WinAlfred.MainWindow"
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:winAlfred="clr-namespace:WinAlfred"
|
||||||
xmlns:winAlfred="clr-namespace:WinAlfred"
|
|
||||||
Title="WinAlfred" Height="80" Width="525"
|
Title="WinAlfred" Height="80" Width="525"
|
||||||
Background="#ebebeb"
|
Background="#ebebeb"
|
||||||
Topmost="True"
|
Topmost="True"
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using WinAlfred.Plugin;
|
using WinAlfred.Plugin;
|
||||||
|
|
||||||
namespace WinAlfred.PluginLoader
|
namespace WinAlfred.PluginLoader
|
||||||
{
|
{
|
||||||
public class PythonPluginLoader : BasePluginLoader
|
public class PythonPluginLoader : BasePluginLoader
|
||||||
{
|
{
|
||||||
|
|
||||||
public override List<PluginPair> LoadPlugin()
|
public override List<PluginPair> LoadPlugin()
|
||||||
{
|
{
|
||||||
List<PluginPair> plugins = new List<PluginPair>();
|
List<PluginPair> plugins = new List<PluginPair>();
|
||||||
@ -21,9 +21,12 @@ namespace WinAlfred.PluginLoader
|
|||||||
};
|
};
|
||||||
plugins.Add(pair);
|
plugins.Add(pair);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (IPlugin plugin in plugins.Select(pluginPair => pluginPair.Plugin))
|
||||||
|
{
|
||||||
|
new Thread(plugin.Init).Start();
|
||||||
|
}
|
||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@ namespace WinAlfred.PluginLoader
|
|||||||
|
|
||||||
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
[DllImport("PyWinAlfred.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||||
private extern static IntPtr ExecPython(string directory, string file, string query);
|
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)
|
public PythonPluginWrapper(PluginMetadata metadata)
|
||||||
{
|
{
|
||||||
@ -27,7 +29,7 @@ namespace WinAlfred.PluginLoader
|
|||||||
|
|
||||||
public void Init()
|
public void Init()
|
||||||
{
|
{
|
||||||
|
InitPythonEnv();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user