I am writing a C program that spawns multiple C threads, with one Python sub-interpreter per thread. The sub-interpreters do not share any mutable Python variables, they are isolated from each other. (They do have a read-only access to a common PyObject (immutable) that is exposed from the main() function in the C program).
Is this possible in Python 3.7 or 3.8, without sharing GIL between the sub-interpreters?
Here is the pseudo-code of what I have been trying:
void *spawnInterpreter(void* p) {
…
PyThreadState* save_tstate = PyThreadState_Swap(NULL);
PyThreadState* tstate = Py_NewInterpreter();
PyThreadState_Swap(save_tstate);
//do some Python work (with variables that are NOT shared with other thread’s sub-interpreter
PyRun_SimpleString( . . .);
. . .
}
int main() {
...
pthread_create(&thread1, NULL, spawnInterpreter, “in1”);
pthread_create(&thread2, NULL, spawnInterpreter, "in2");
...
}
I could get this to work in 3.6 (without acquiring GIL or managing PyThreadState in C threads), but in Python 3.7 I get:
[New Thread 0x7ffff5f78700 (LWP 16392)]
Fatal Python error: drop_gil: GIL is not locked