<div>@Julien</div><div>I have been calling Python from C# without any problems even in a multi-threaded setup. I am using .Net 2.0 and Python 2.5. Main difference, at least from what I don't see in your code, is I also call BeginAllowThreads() after Initialize(). This allows me to initialize PythonEngine in a different thread once and never have to worry about it again.</div>
<div><br></div><div>I remember having similar problem when I first embedded Python and this doc helped me come up with this solution.</div><div><a href="http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock">http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock</a></div>
<div><br></div><div>BTW, AcquireLock() below always returns 1.</div><div><br></div><div>Please let me know if this helps.</div><div><br></div><div>/* This is in main thread */</div><div><div>if (!PythonEngine.IsInitialized) {</div>
<div> PythonEngine.Initialize();</div><div> PythonEngine.BeginAllowThreads();</div><div>}</div></div><div><div> </div><div>/* This is ran in another thread */</div><div>IntPtr gs = PythonEngine.AcquireLock(); </div>
<div><br></div><div>PyObject module = PythonEngine.ImportModule(pymodule);</div><div>try {</div><div> PyObject res = module.InvokeMethod(method_name, _args);</div><div> /* Python calls here */</div><div> ....</div>
<div>} catch (PythonException pe) {</div><div> /* Handle python exceptions */</div><div>} finally {</div><div> PythonEngine.ReleaseLock(gs);</div><div>}</div></div>