Hello,

 

After a few days of headache, I think the best way is to share this
problematic…

 

I have :

- a main Python script

- a C module imported and called from Python  (thanks to boost ::python)

- A dozen of threads created by a C function  (boost ::thread)

- These threads, in a C main loop, call regularly a Python function.

 

 

Here is the critical part of the C code, with the Python GIL management :

 

boost::mutex      _pyMutex;

PyGILState_STATE  _gstate;

 

_pyMutex.lock();

_gstate = PyGILState_Ensure();

_pyMutex.unlock();

 

// Interprets Python bytecodes with boost::python

 

_pyMutex.lock();

PyGILState_Release(_gstate);

_pyMutex.unlock();

 

 

This part is multi-threaded, and it seems that the PyGILState_* functions do
not like it.

I have the following error with PyGILState_Release : « This thread state
must be current when releasing »

 

I think that when 2 threads enter PyGILState_Ensure, the second one is
considered as the current one. When the first one end…  it is not current. A
quick look to the code of the Python API seems to confirm it.

 

 

So, the only solution I found is to lock the whole call to Python :

 

boost::mutex      _pyMutex; 

PyGILState_STATE  _gstate;

 

_pyMutex.lock();

_gstate = PyGILState_Ensure();

 

// Interprets Python bytecodes with boost::python

 

PyGILState_Release(_gstate);

_pyMutex.unlock();

 

 

Everything is ok with this solution, except that…  only one thread at a time
can call a Python method.

When 2 threads ask for a Python call, if the first one is a blocking
operation, the second one will wait…  it is so bad !

 

Do you have any solution ?

 

Nb : I use Python 2.5.2

 

Thanks for your help.

 

 

_____

 

Briet Pascal

Ingénieur d'études

 

+33 (0)1 47 42 10 55

 

OptimProcess

32 rue Tronchet - 75009 Paris 
 <http://www.optimprocess.com/> www.optimprocess.com

 





 

<<image001.jpg>>

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to