Running multiprocess python module from C#

67 Views Asked by At

Multiprocess Custom Python module ("area.py")

I have a custom python module that runs CPU bound task on multicores using multiprocess python library. This function also has some numpy and scipy libraries for calculating trapezoidal or simpson's rule.

Main C# Application**

I have to call the python module from the C# (.NET Framework 4.8) and I was looking for options and thought of the 3 possible options in the order of preference.

pythonnet

  • Supports Python and recommended by .NET foundation and hence this was my first choice.

  • However it looks like they don't support multiprocess as they use GIL lock from below example- GitHub

  • I have my pseudo code written below. When I run the multiprocess "area.py" file, my system crashes and I cannot run without Py.GIL() statement enabled as that throws memory protection error.

static void Main(string[] args)
{
    PythonEngine.Initialize();
    using (Py.GIL())
    {
        var script= Py.Import("area");
        script.InvokeMethod("area");

    }
}
  • Is there a way to run multiprocess py module using pythonnet?

IronPython

  • Next option is to use IronPython which also supports multithreading but I see most of the articles suggesting to avoid this as this may soon become obsolete and also the updates are slow. (still stuck at python 3.4).

Executable

  • My last approach would be run the area.py files as an executable rather than .py module or convert the existing python module to C#.

Next Step

  • I have so far tried Pythonnet option.
  • It runs fine with single process python module but fails with multi process python module file.
  • What is the best option in this situation?
0

There are 0 best solutions below