[Python-Dev] Py_NewInterpreter and PyGILState API

Christoph Wiedemann wiedeman at gmx.net
Mon Sep 27 09:39:30 CEST 2004


Hello,

first of all my apologies for sending this message to python-dev, but i
tried comp.lang.pthon and python-help and didn't get helpful answers.

My problem is, that Py_NewInterpreter and the in 2.3 introduced PyGILState
API doesn't play nice with each other, especially in multithreaded embedding
applications. I think, this is because PyGILState functions assume, there is
exactly one PyThreadState instance per thread, and this is violated when
using Py_NewInterpreter, which creates a new PyThreadState instance.

I've tried to use 

state = PyGILState_Ensure();
PyThreadState_Get()->interp = interpreterIWantToUse;
/* code using Python API */
PyGILState_Release(state);

which seems to work, if called from one thread only, but this fails, if used
by multiple threads with a "Fatal Python error: PyThreadState_Delete:
invalid tstate."

Now, most of you would say: "Don't use PyGILState API, use the 2.2 way of
dealing with thread states". Unfortunately, i want to use PyQt, which uses
the PyGILState API, and i found, that it's not easy (or even impossible?) to
mix PyGILState calls with PyEval_SaveThread / PyEval_RestoreThread.

I'm lost with this and would appreciate any help. I'm using Python 2.3 on
linux x86.

Christoph



More information about the Python-Dev mailing list