diff --git a/OpenRPA.Script/Activities/InvokeCode.cs b/OpenRPA.Script/Activities/InvokeCode.cs index 7bf2354a..57ac039b 100644 --- a/OpenRPA.Script/Activities/InvokeCode.cs +++ b/OpenRPA.Script/Activities/InvokeCode.cs @@ -371,133 +371,152 @@ public void Execute(CodeActivityContext context, string code, string language, D _ = Python.Runtime.PythonEngine.BeginAllowThreads(); python_doinit = false; } - lck = PythonEngine.AcquireLock(); + // lck = PythonEngine.AcquireLock(); doRelease = true; - using (var scope = Py.CreateScope()) + using (Python.Runtime.Py.GIL()) { - foreach (var parameter in variablevalues) - { - PyObject pyobj = parameter.Value.ToPython(); - scope.Set(parameter.Key, pyobj); - } - try + //// create a Python scope + using (var scope = Python.Runtime.Py.CreateScope()) { + foreach (var parameter in variablevalues) + { + PyObject pyobj = parameter.Value.ToPython(); + scope.Set(parameter.Key, pyobj); + } + try + { - PythonOutput output = new PythonOutput(); - dynamic sys = Py.Import("sys"); - sys.stdout = output; - sys.stderr = output; - - // PythonEngine.RunSimpleString(@" - //import sys - //from System import Console - //class output(object): - // def write(self, msg): - // Console.Out.Write(msg) - // def writelines(self, msgs): - // for msg in msgs: - // Console.Out.Write(msg) - // def flush(self): - // pass - // def close(self): - // pass - //sys.stdout = sys.stderr = output() - //"); + PythonOutput output = new PythonOutput(); + dynamic sys = Py.Import("sys"); + sys.stdout = output; + sys.stderr = output; + + // PythonEngine.RunSimpleString(@" + //import sys + //from System import Console + //class output(object): + // def write(self, msg): + // Console.Out.Write(msg) + // def writelines(self, msgs): + // for msg in msgs: + // Console.Out.Write(msg) + // def flush(self): + // pass + // def close(self): + // pass + //sys.stdout = sys.stderr = output() + //"); - } - catch (Exception _ex) - { - Log.Debug(_ex.ToString()); - } - scope.Exec(code); - foreach (var parameter in variablevalues) - { - PyObject pyobj = scope.Get(parameter.Key); - if (pyobj == null) continue; - if (Arguments == null || Arguments.Count == 0) + } + catch (Exception _ex) { - PropertyDescriptor myVar = context.DataContext.GetProperties().Find(parameter.Key, true); - if (myVar == null) continue; - if (myVar.PropertyType == typeof(string)) - myVar.SetValue(context.DataContext, pyobj.ToString()); - else if (myVar.PropertyType == typeof(int)) myVar.SetValue(context.DataContext, int.Parse(pyobj.ToString())); - else if (myVar.PropertyType == typeof(bool)) myVar.SetValue(context.DataContext, bool.Parse(pyobj.ToString())); - else - { - try - { - var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), myVar.PropertyType); - myVar.SetValue(context.DataContext, obj); - } - catch (Exception _ex) - { - Log.Information("Failed variable " + parameter.Key + " of type " + myVar.PropertyType.FullName + " " + _ex.Message); - } - } + Log.Debug(_ex.ToString()); } - else + scope.Exec(code); + foreach (var parameter in variablevalues) { - foreach (var a in Arguments) + PyObject pyobj = scope.Get(parameter.Key); + if (pyobj == null) continue; + if (Arguments == null || Arguments.Count == 0) { - if (a.Key == parameter.Key) + PropertyDescriptor myVar = context.DataContext.GetProperties().Find(parameter.Key, true); + if (myVar == null) continue; + if (myVar.PropertyType == typeof(string)) + myVar.SetValue(context.DataContext, pyobj.ToString()); + else if (myVar.PropertyType == typeof(int)) myVar.SetValue(context.DataContext, int.Parse(pyobj.ToString())); + else if (myVar.PropertyType == typeof(bool)) myVar.SetValue(context.DataContext, bool.Parse(pyobj.ToString())); + else { - if (pyobj == null) - { - Arguments[a.Key].Set(context, null); - } - else if (a.Value.ArgumentType == typeof(string)) + try { - Arguments[a.Key].Set(context, pyobj.ToString()); + var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), myVar.PropertyType); + myVar.SetValue(context.DataContext, obj); } - else if (a.Value.ArgumentType == typeof(int)) + catch (Exception _ex) { - Arguments[a.Key].Set(context, int.Parse(pyobj.ToString())); + Log.Information("Failed variable " + parameter.Key + " of type " + myVar.PropertyType.FullName + " " + _ex.Message); } - else if (a.Value.ArgumentType == typeof(float)) - { - Arguments[a.Key].Set(context, float.Parse(pyobj.ToString())); - } - else if (a.Value.ArgumentType == typeof(bool)) - { - Arguments[a.Key].Set(context, bool.Parse(pyobj.ToString())); - }else if(a.Value.ArgumentType == typeof(object)) + } + } + else + { + foreach (var a in Arguments) + { + if (a.Key == parameter.Key) { - if (PyString.IsStringType(pyobj)) + if (pyobj == null) { - Arguments[a.Key].Set(context, pyobj.ToString()); + Arguments[a.Key].Set(context, null); } - else if (PyNumber.IsNumberType(pyobj) && ("True" == pyobj.ToString() || "False" == pyobj.ToString())) + else if (a.Value.ArgumentType == typeof(string)) { - Arguments[a.Key].Set(context, bool.Parse(pyobj.ToString())); + Arguments[a.Key].Set(context, pyobj.ToString()); } - else if (PyInt.IsIntType(pyobj)) + else if (a.Value.ArgumentType == typeof(int)) { Arguments[a.Key].Set(context, int.Parse(pyobj.ToString())); } - else if (PyFloat.IsFloatType(pyobj)) + else if (a.Value.ArgumentType == typeof(float)) { Arguments[a.Key].Set(context, float.Parse(pyobj.ToString())); - }else if (PyDict.IsDictType(pyobj)) + } + else if (a.Value.ArgumentType == typeof(bool)) { - try + Arguments[a.Key].Set(context, bool.Parse(pyobj.ToString())); + } + else if (a.Value.ArgumentType == typeof(object)) + { + if (PyString.IsStringType(pyobj)) { - var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), typeof(JObject)); - Arguments[a.Key].Set(context, obj); + Arguments[a.Key].Set(context, pyobj.ToString()); } - catch (Exception _ex) + else if (PyNumber.IsNumberType(pyobj) && ("True" == pyobj.ToString() || "False" == pyobj.ToString())) { - Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); + Arguments[a.Key].Set(context, bool.Parse(pyobj.ToString())); } - }else if(PyList.IsListType(pyobj)) - { - try + else if (PyInt.IsIntType(pyobj)) { - var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), typeof(JArray)); - Arguments[a.Key].Set(context, obj); + Arguments[a.Key].Set(context, int.Parse(pyobj.ToString())); } - catch (Exception _ex) + else if (PyFloat.IsFloatType(pyobj)) { - Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); + Arguments[a.Key].Set(context, float.Parse(pyobj.ToString())); + } + else if (PyDict.IsDictType(pyobj)) + { + try + { + var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), typeof(JObject)); + Arguments[a.Key].Set(context, obj); + } + catch (Exception _ex) + { + Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); + } + } + else if (PyList.IsListType(pyobj)) + { + try + { + var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), typeof(JArray)); + Arguments[a.Key].Set(context, obj); + } + catch (Exception _ex) + { + Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); + } + } + else + { + try + { + var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), a.Value.ArgumentType); + Arguments[a.Key].Set(context, obj); + } + catch (Exception _ex) + { + Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); + } } } else @@ -513,27 +532,15 @@ public void Execute(CodeActivityContext context, string code, string language, D } } } - else - { - try - { - var obj = Newtonsoft.Json.JsonConvert.DeserializeObject(pyobj.ToString(), a.Value.ArgumentType); - Arguments[a.Key].Set(context, obj); - } - catch (Exception _ex) - { - Log.Information("Failed variable " + parameter.Key + " of type " + a.Value.ArgumentType.FullName + " " + _ex.Message); - } - } } + } } - } + //lck = PythonEngine.AcquireLock(); + //PythonEngine.Exec(code); } - //lck = PythonEngine.AcquireLock(); - //PythonEngine.Exec(code); } catch (Exception _ex) { @@ -541,7 +548,7 @@ public void Execute(CodeActivityContext context, string code, string language, D } finally { - if (doRelease) PythonEngine.ReleaseLock(lck); + // if (doRelease) PythonEngine.ReleaseLock(lck); } }); //using (Python.Runtime.Py.GIL()) diff --git a/OpenRPA.Script/OpenRPA.Script.csproj b/OpenRPA.Script/OpenRPA.Script.csproj index 3432dcea..821b8949 100644 --- a/OpenRPA.Script/OpenRPA.Script.csproj +++ b/OpenRPA.Script/OpenRPA.Script.csproj @@ -87,7 +87,7 @@ - +