<div dir="ltr">1. Jian, can you convert your code to simplified interface using Py.GIL() calls? If not possible, please let us know why.<div><br></div><div><a href="https://github.com/pythonnet/pythonnet/blob/master/README.md" target="_blank">https://github.com/pythonnet/pythonnet/blob/master/README.md</a><br><div><br></div><div>2. Your first code sample (unit test) is self-contained, but in second code sample (UI) it is not clear where you acquire and release the GIL?</div></div><div><br></div><div>3. Finally can you post this as an issue on github?</div><div><br></div><div><a href="https://github.com/pythonnet/pythonnet/issues/new">https://github.com/pythonnet/pythonnet/issues/new</a><br></div><div><br></div><div>Thanks,</div><div>Denis</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 28, 2016 at 8:51 AM, Jian Wang <span dir="ltr"><<a href="mailto:wjchicago@gmail.com" target="_blank">wjchicago@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear PythonNet group,<div><br></div><div>I am working on a c# project which would like to access functions written in python from time to time. Below is a wrapper function.</div><div><br></div><div>I have a unittest setup for this function. Everytime I run this unittest, it will get information from python world as I desired. </div><div><br></div><div><span style="font-size:x-small">        public ScriptContext GetContextFromScript()</span><br></div><div><div><font size="1">        {</font></div><div><font size="1">            ScriptContext scriptContext;<br></font></div><div><font size="1">            try</font></div><div><font size="1">            {</font></div><div><font size="1">                if (!PythonEngine.IsInitialized)</font></div><div><font size="1">                    PythonEngine.Initialize();</font></div><div><font size="1">                var threadState = PythonEngine.BeginAllowThreads();</font></div><div><font size="1">                var gilState = PythonEngine.AcquireLock();</font></div><div><font size="1"><br></font></div><div><font size="1">                PyObject mod = PythonEngine.ImportModule(this.GetModuleName());</font></div><div><font size="1">                PyObject instance = mod.InvokeMethod("Strategy", new PyTuple());</font></div><div><font size="1">                scriptContext =(ScriptContext)instance.InvokeMethod("SetContext", new PyTuple()).AsManagedObject(typeof (ScriptContext));</font></div><div><font size="1"><br></font></div><div><font size="1">                PythonEngine.ReleaseLock(gilState);</font></div><div><font size="1">                PythonEngine.EndAllowThreads(threadState);</font></div><div><font size="1">                PythonEngine.Shutdown();</font></div><div><span style="font-size:x-small">            }</span><br></div><div><font size="1">            catch (Exception ex)</font></div><div><font size="1">            {</font></div><div><font size="1">                throw;</font></div><div><font size="1">            }</font></div><div><span style="font-size:x-small">            return scriptContext;</span><br></div><div><font size="1">        }</font></div></div><div><font size="1"><br></font></div><div><div>However, when I launch the UI and call this function by clicking some button, the program hangs at <span style="font-size:x-small"> PythonEngine.Initialize().</span></div><div><br></div><div>After debugging into the PythonNet source code, I found when calling through UI, the RunTime.Initialize() will return true for Runtime.Py_IsInitialized(). </div></div><div><br></div><div><div><font size="1">        internal static void Initialize()</font></div><div><font size="1">        {</font></div><div><font size="1">            is32bit = IntPtr.Size == 4;</font></div><div><font size="1"><br></font></div><div><font size="1">            if (0 == Runtime.Py_IsInitialized())</font></div><div><font size="1">            {</font></div><div><font size="1">                Runtime.Py_Initialize();</font></div><div><font size="1">            }</font></div><div><font size="1"><br></font></div><div><font size="1">            if (0 == Runtime.PyEval_ThreadsInitialized())</font></div><div><font size="1">            {</font></div><div><font size="1">                Runtime.PyEval_InitThreads();</font></div><div><font size="1">            }</font></div></div><div>      <font size="1">}</font></div><div><font size="1"><br></font></div><div>And the whole initialize process will hang when result.Dispose() is called. It actually trying to acquire lock but cannot get it.</div><div><font size="1"><br></font></div><div><div style="font-size:x-small">                    using (StreamReader reader = new StreamReader(stream))</div><div style="font-size:x-small">                    {</div><div style="font-size:x-small">                        // add the contents of clr.py to the module</div><div style="font-size:x-small">                        string clr_py = reader.ReadToEnd();</div><div style="font-size:x-small">                        PyObject result = RunString(clr_py, module_globals, locals.Handle);</div><div style="font-size:x-small">                        if (null == result)</div><div style="font-size:x-small">                            throw new PythonException();</div><div style="font-size:x-small">                        result.Dispose();</div><div style="font-size:x-small">                    }</div><div style="font-size:x-small"><br></div><div>It looks like something happend before code reaching this pythong wrapper function. PythonEninge is partially initialized with Runtime.Py_IsInitialized() = true and Runtime.PyEval_ThreadsInitialized() = false. </div></div><div><br></div><div>Questions</div><div>1. what could be the possible cause for this situation? </div><div>2. If I cannot find the root cause and put BegineAllowThreads() there, what could be a solution for it?</div><div><br></div><div>Many thanks!</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Jian</div></font></span></div>
<br>_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org">PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><br></blockquote></div><br></div>