public unsafe static int Initialize(IntPtr data, int size) { try { var dllPath = Encoding.UTF8.GetString((byte *)data.ToPointer(), size); if (!string.IsNullOrEmpty(dllPath)) { PythonDLL = dllPath; } else { PythonDLL = null; } using var _ = Py.GIL(); PythonEngine.InitExt(); } catch (Exception exc) { Console.Error.Write( $"Failed to initialize pythonnet: {exc}\n{exc.StackTrace}" ); return(1); } return(0); }
public unsafe static int Initialize(IntPtr data, int size) { IntPtr gs = IntPtr.Zero; try { var dllPath = Encoding.UTF8.GetString((byte *)data.ToPointer(), size); if (!string.IsNullOrEmpty(dllPath)) { PythonDLL = dllPath; } else { PythonDLL = null; } gs = PyGILState_Ensure(); // Console.WriteLine("Startup thread"); PythonEngine.InitExt(); // Console.WriteLine("Startup finished"); } catch (Exception exc) { Console.Error.Write( $"Failed to initialize pythonnet: {exc}\n{exc.StackTrace}" ); return(1); } finally { if (gs != IntPtr.Zero) { PyGILState_Release(gs); } } return(0); }