1

I am using pythonnet in a c#-application successfully. Now I am trying to call pythonnet code from a parallel.for loop.

The regular for-loop works fine but not when parallel is used. The programm is not responding and not showing an error message.

(I tried different ways to do this as well including calling the Py.GIL explicitly and using threading. But couldnt get it to run)

Here is a simplified version of my code:

    static void Main(string[] args)
    {
        PythonEngine.Initialize();
        dynamic np = PythonEngine.ImportModule("numpy");

        dynamic[] Output = new dynamic[10];
        dynamic[] Output2 = new dynamic[10];

        for (int i = 0; i < 10; i++)
        {
            Output[i] = np.cos(np.pi * i);
        }

        Parallel.For(0, 10, i =>
        {
            Output2[i] = np.cos(np.pi * 2);
        });

        Console.ReadLine();
    }

1 Answers1

0

Short Answer: (Your) Python is single-threaded.

Long Answer: A quick Google search for PythonEngine.ImportModule yields the relevant file in the github repo and that np is a PyObject. From the pyobject.cs and runtime.cs files in the same directory, we can see that calling np.cos is thread-independent except for a PInvoke to the external Python library. From the PythonNet project page http://pythonnet.github.io/, it binds against CPython. However, CPython has a global lock on the interpreter; see Does Python support multithreading? Can it speed up execution time?.

Possible solution: If you need to use Python, try with a different runtime (e.g. IronPython, which is native on dotnet).

mostanes
  • 149
  • 6