research python plugin: seems ok in multiple thread now.

This commit is contained in:
qianlifeng
2014-01-11 15:02:17 +08:00
parent 76009ca6eb
commit 73867d87d7
7 changed files with 85 additions and 47 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@@ -2,7 +2,7 @@
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import json import json
import webbrowser
def query(key): def query(key):
k = key.split(" ")[1] k = key.split(" ")[1]
@@ -15,15 +15,18 @@ def query(key):
res = {} res = {}
title = i.select("a")[0].text.replace("\n","").replace(" ","") title = i.select("a")[0].text.replace("\n","").replace(" ","")
score = i.select("span.rating_nums")[0].text if i.select("span.rating_nums") else "0" score = i.select("span.rating_nums")[0].text if i.select("span.rating_nums") else "0"
res["Title"] = title res["Title"] = title.split("/")[0]
res["SubTitle"] = score year = i.select("p.pl")[0].text.split("-")[0] if i.select("p.pl")[0] else "Null"
alias = title.split("/")[1] if len(title.split("/")) >= 2 else "Null"
res["SubTitle"] = "Year: " + year + " Score: " + score + " Alias: " + alias
res["ActionName"] = "openUrl" res["ActionName"] = "openUrl"
res["IcoPath"] = "Images\\movies.png"
res["ActionPara"] = i.select("a[href]")[0]["href"] res["ActionPara"] = i.select("a[href]")[0]["href"]
results.append(res) results.append(res)
return json.dumps(results) return json.dumps(results)
def openUrl(url): def openUrl(url):
pass webbrowser.open(url)
if __name__ == "__main__": if __name__ == "__main__":
print query("movie geo") print query("movie geo")

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using Python.Runtime;
using WinAlfred.Helper; using WinAlfred.Helper;
using WinAlfred.Plugin; using WinAlfred.Plugin;
using WinAlfred.PluginLoader; using WinAlfred.PluginLoader;
@@ -9,6 +11,9 @@ namespace WinAlfred.Commands
{ {
public class PluginCommand : BaseCommand public class PluginCommand : BaseCommand
{ {
private string currentPythonModulePath = string.Empty;
private IntPtr GIL;
public PluginCommand(MainWindow mainWindow) public PluginCommand(MainWindow mainWindow)
: base(mainWindow) : base(mainWindow)
{ {
@@ -17,36 +22,56 @@ namespace WinAlfred.Commands
public override void Dispatch(Query q) public override void Dispatch(Query q)
{ {
PluginPair thirdPlugin = Plugins.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == q.ActionName);
foreach (PluginPair pair in Plugins.AllPlugins) if (thirdPlugin != null && !string.IsNullOrEmpty(thirdPlugin.Metadata.ActionKeyword))
{ {
if (pair.Metadata.ActionKeyword == q.ActionName) if (thirdPlugin.Metadata.Language == AllowedLanguage.Python)
{ {
PluginPair pair1 = pair; SwitchPythonEnv(thirdPlugin);
ThreadPool.QueueUserWorkItem(state =>
{
try
{
List<Result> r = pair1.Plugin.Query(q);
r.ForEach(o =>
{
o.PluginDirectory = pair1.Metadata.PluginDirecotry;
o.OriginQuery = q;
});
UpdateResultView(r);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", pair1.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
}
});
} }
ThreadPool.QueueUserWorkItem(t =>
{
try
{
List<Result> r = thirdPlugin.Plugin.Query(q);
r.ForEach(o =>
{
o.PluginDirectory = thirdPlugin.Metadata.PluginDirecotry;
o.OriginQuery = q;
});
UpdateResultView(r);
}
catch (Exception queryException)
{
Log.Error(string.Format("Plugin {0} query failed: {1}", thirdPlugin.Metadata.Name,
queryException.Message));
#if (DEBUG)
{
throw;
}
#endif
}
});
}
}
private void SwitchPythonEnv(PluginPair thirdPlugin)
{
if (currentPythonModulePath != thirdPlugin.Metadata.PluginDirecotry)
{
//this must initial in main thread
currentPythonModulePath = thirdPlugin.Metadata.PluginDirecotry;
if (GIL != IntPtr.Zero)
{
Runtime.PyEval_RestoreThread(GIL);
PythonEngine.Shutdown();
}
PythonEngine.Initialize();
IntPtr pyStrPtr = Runtime.PyString_FromString(thirdPlugin.Metadata.PluginDirecotry);
IntPtr SysDotPath = Runtime.PySys_GetObject("path");
Runtime.PyList_Append(SysDotPath, pyStrPtr);
GIL = PythonEngine.BeginAllowThreads();
} }
} }
} }

View File

@@ -45,7 +45,7 @@ namespace WinAlfred
void MainWindow_Closed(object sender, EventArgs e) void MainWindow_Closed(object sender, EventArgs e)
{ {
PythonEngine.Shutdown(); //PythonEngine.Shutdown();
} }
private void InitProgressbarAnimation() private void InitProgressbarAnimation()

View File

@@ -117,5 +117,16 @@ namespace WinAlfred.PluginLoader
return null; return null;
} }
} }
///// <summary>
///// Change python execute file name to unique file name using GUID
///// this is because if two pythong plugin use the same
///// </summary>
///// <param name="metadata"></param>
///// <returns></returns>
//private static PluginMetadata filterPythonMetadata(PluginMetadata metadata)
//{
//}
} }
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using Python.Runtime; using Python.Runtime;
@@ -11,8 +12,7 @@ namespace WinAlfred.PluginLoader
static PythonPluginLoader() static PythonPluginLoader()
{ {
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
} }
public override List<PluginPair> LoadPlugin() public override List<PluginPair> LoadPlugin()

View File

@@ -10,6 +10,7 @@ namespace WinAlfred.PluginLoader
{ {
public class PythonPluginWrapper : IPlugin public class PythonPluginWrapper : IPlugin
{ {
private PluginMetadata metadata; private PluginMetadata metadata;
public PythonPluginWrapper(PluginMetadata metadata) public PythonPluginWrapper(PluginMetadata metadata)
@@ -21,13 +22,13 @@ namespace WinAlfred.PluginLoader
{ {
try try
{ {
string s = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""),"query",query.RawQuery); string jsonResult = InvokeFunc(metadata.PluginDirecotry, metadata.ExecuteFileName.Replace(".py", ""), "query", query.RawQuery);
if (string.IsNullOrEmpty(s)) if (string.IsNullOrEmpty(jsonResult))
{ {
return new List<Result>(); return new List<Result>();
} }
List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(s); List<PythonResult> o = JsonConvert.DeserializeObject<List<PythonResult>>(jsonResult);
List<Result> r = new List<Result>(); List<Result> r = new List<Result>();
foreach (PythonResult pythonResult in o) foreach (PythonResult pythonResult in o)
{ {
@@ -42,22 +43,20 @@ namespace WinAlfred.PluginLoader
} }
catch (Exception) catch (Exception)
{ {
#if (DEBUG)
throw; {
throw;
}
#endif
} }
} }
private string InvokeFunc(string path, string moduleName,string func, string para) private string InvokeFunc(string path, string moduleName, string func, string para)
{ {
IntPtr gs = PythonEngine.AcquireLock(); IntPtr gs = PythonEngine.AcquireLock();
IntPtr pyStrPtr = Runtime.PyString_FromString(path);
IntPtr SysDotPath = Runtime.PySys_GetObject("path");
Runtime.PyList_Append(SysDotPath, pyStrPtr);
PyObject module = PythonEngine.ImportModule(moduleName); PyObject module = PythonEngine.ImportModule(moduleName);
module = PythonEngine.ReloadModule(module);
PyObject res = module.InvokeMethod(func, new PyString(para)); PyObject res = module.InvokeMethod(func, new PyString(para));
string json = Runtime.GetManagedString(res.Handle); string json = Runtime.GetManagedString(res.Handle);