[Python.Net] AcquireLock deadlock while calling Python from C#

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Python.Net] AcquireLock deadlock while calling Python from C#

Benjamin Aranguren
@Julien
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.

I remember having similar problem when I first embedded Python and this doc helped me come up with this solution.

BTW, AcquireLock() below always returns 1.

Please let me know if this helps.

/* This is in main thread */
if (!PythonEngine.IsInitialized) {
    PythonEngine.Initialize();
    PythonEngine.BeginAllowThreads();
}
            
/* This is ran in another thread */
IntPtr gs = PythonEngine.AcquireLock();            

PyObject module = PythonEngine.ImportModule(pymodule);
try {
    PyObject res = module.InvokeMethod(method_name, _args);
    /* Python calls here */
   ....
} catch (PythonException pe) {
    /* Handle python exceptions */
} finally {
    PythonEngine.ReleaseLock(gs);
}

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet