
Hello, I create lots of threads that run a script and I have a problem when a thread is created with the same threadId that existed before.
When a new thread is started I use this code to enter the interpreter: // create a thread state object for this thread PyEval_AcquireLock(); threadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(threadState);
and when the thread finishes:
PyThreadState_Swap(NULL); PyThreadState_Clear(info); // delete my thread state object PyThreadState_Delete(info); PyEval_ReleaseLock();
Everything works smoothly until a thread id is re-used. I've been testing what is happening and the problem is that PyGILState_GetThisThreadState() returns a PyThreadState for the reused thread id. I changed my code to call PyGILState_GetThisThreadState() before creating the PyThreadState. But if I try to use the returned PyThreadState (that shouldn't exist because I've already deleted) I got an error when trying to access the interpreter dictionary.
The workaround that I found is to keep all PyThreadState structures without calling PyThreadState_Clear / PyThreadState_Delete. When a new thread is created I call PyGILState_GetThisThreadState(). If it returns something I reuse the structure.
In this way, my code works.
Thanks, Pablo Yabo
participants (1)
-
Pablo Yabo