I'm trying to use pythonnet (C# library) from multiple threads, but it deadlocks. The following seems to be the procedure for calling CPython from that library:
Py_InitializeEx(0); // Initialize CPython
if (PyEval_ThreadsInitialized() == 0)
PyEval_InitThreads();
// ---------------------------
gs = PyGILState_Ensure();
// Execute some Python code here
PyGILState_Release(gs);
However, when running this again from a different thread (not main thread) it deadlocks:
gs = PyGILState_Ensure(); // Deadlocks!
// Execute some Python code here
PyGILState_Release(gs);
What is the proper way to call CPython from multiple threads?
The following question:
Embedding python in multithreaded C application
seems to be related, but I am non the wiser in what the "correct" approach is?