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

Julien faixo julien.faixo at gmail.com
Wed Dec 1 15:16:10 CET 2010


Hi,

As Python offers better scientific computing libraries, I wish to reate a
bridge between Python and C#. Numpy and matplotlib being some modules
needed, I choosed Python.Net rather than IronPython in order to make this
work (I'm aware of the existence of IronClad, but unfortunately it seems not
to be fully compatible with matplotlib and numpy...).

Using Python.Net, I've been quite quickly able to launch Python scripts with
parameters, and get back script outputs in C#. That's pretty amazing, and
before poiting out my problem, I'd like to thank all the Python.Net staff,
that's a really nice project :)

Now, let's talk dirty: my problem is as I said I'm able to launch python
script, but this can happen only once. I am using AcquireLock and
ReleaseLock as explained on various websites, but AcquireLock returns in
IntPtr containing "0" (is it a bug ?), and on the second call to
AcquireLock, that's it baby, a deadlock :(

PythonEngine.Initialize() has been previously called, and is called only
once.

I'm using .Net 2.0, and this issue looks exactly like this
one<http://www.mail-archive.com/pythondotnet@python.org/msg00299.html>that
is 4 years old *sic*

Below, the piece of code called twice.

  IntPtr gs = PythonEngine.AcquireLock();
>             try
>             {
>                 PyObject module = PythonEngine.ImportModule(moduleName);
>                 PyObject method = module.GetAttr("Invoke");
>                 PyObject[] items = new PyObject[parameters.Length];
>                 for (int i = 0; i < parameters.Length; ++i)
>                     items[i] = ToPython(parameters[i]);
>
>                 PyObject pythonReturnValue = method.Invoke(new
> PyTuple(items));
>
>                 Object[] result;
>                 if (pythonReturnValue.Length() > 0)
>                 {
>                     result = new Object[pythonReturnValue.Length()];
>                     for (int i = 0; i < pythonReturnValue.Length(); ++i)
>                         result[i] =
> pythonReturnValue.GetItem(i).AsManagedObject(typeof(double));
>                 }
>                 else
>                 {
>                     result = new Object[1];
>                     result[0] =
> pythonReturnValue.AsManagedObject(typeof(double));
>                 }
>
>                 module.Dispose();
>                 pythonReturnValue.Dispose();
>                 foreach (PyObject item in items)
>                     item.Dispose();
>
>                 return result;
>             }
>             finally
>             {
>                 PythonEngine.ReleaseLock(gs);
>             }
>

Is there something I may have done wrong ?

Thanks for your help,
Julien
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20101201/2b6d3b1a/attachment-0001.html>


More information about the PythonDotNet mailing list