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 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);
}