Jump to content
  • Advertisement
Sign in to follow this  
dcosborn

[Python/C] Threads and GIL States

This topic is 5412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a Python extension module in C. I've got a signal/slot mechanism where callable Python objects/functions can hook themselves up to signals that have been created either through Python or from within the extension, possibly from threads that the Python interpreter doesn't know about. From what I've read, the PyGILState_Ensure and PyGILState_Release functions should allow Python code to be executed from an environment where the state of the Python interpreter is unknown. Anyway, my callbacks are run through a router that looks like this:
static void router(unsigned int index)
{
	PyGILState_STATE gstate = PyGILState_Ensure();
	PyObject *args = PyTuple_New(0);
	PyObject_Call(slots[index].handler, args, 0); // Calls the Python callback function.
	Py_DECREF(args);
	PyGILState_Release(gstate);
}





When I run the program, the Python interpreter dies with a "Fatal Python error: PyThreadState_Get: no current thread". I figured PyGILState_Release might be killing off the main thread state by accident, so I commented it out and woila: it ran without problems. However, it shouldn't be like that. Note that I don't get these problems when I call the signal from within the Python program; it only happens when I call it from a hidden thread that Python doesn't know about. Thanks and let me know if I'm doing something dumb. I can't seem to find a whole lot of information about the PyGILState functions, maybe because they're so new.

Share this post


Link to post
Share on other sites
Advertisement
Well I seem to have stumbled across the solution. Apparantly I have to call PyEval_InitThreads() before I can use the PyGILState functions. I would have thought Python would have called that function on startup by itself before starting to run the program...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!