My Blazor application is crashing when I run a function using the Python.NET methods, whilst another one is running. Here is the code in question:
try{ Runtime.PythonDLL = @"/usr/lib/python3.10/config-3.10-x86_64-linux-gnu/libpython3.10.so"; if (!PythonEngine.IsInitialized) { PythonEngine.Initialize(); var threadState = PythonEngine.BeginAllowThreads(); }}catch (Exception e){ await _js.InvokeVoidAsync("console.log", e.Message); return;}using (Py.GIL()){ dynamic sys = Py.Import("sys"); sys.path.append(@"/var/www/html/DBAdminTest/wwwroot/python/transform"); var scope = Py.CreateScope(); /* Python script and variable handling */ try { scope.Exec(script, new PyDict()); } catch (Exception ex) { await _js.InvokeVoidAsync("console.log", ex.Message); } scope.Dispose();}PythonEngine.Shutdown();await _js.InvokeVoidAsync("console.log", "shutdown");The code above works great when I run a script on its own and even when I run it multiple times.
However, this causes an exception and the application crashes when I run the function while another instance of it is already running. This is the error message:
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]Unhandled exception in circuit '2PDfJXSqgTZikWfpUPVcLfrkGzen7ao5nbpp2VQBanY'.System.InvalidOperationException: This property must be set before runtime is initializedat Python.Runtime.Runtime.set_PythonDLL(String value)I don't know what my mistake in the implementation of the code is, so I would be grateful if anyone could help out!