Threading

Pablo Yabo pablo.yabo at gmail.com
Fri Aug 10 19:31:37 EDT 2007


Hello,

I'm trying to
call Python routines from C/C++. These routines must intereact in a
multi-thread environment.
I mean that routine A will loop waiting for a condition of routine B.

This condition are coded in C so I don't need
routine A running in the same interpreter of routine B.

I tried using:

PyEval_AcquireLock();
PyInterpreterState* mainInterpreterState = mainThreadState->interp;
PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);

PyThreadState_Swap(myThreadState);

... Call Python code here

PyThreadState_Swap(NULL);
PyEval_ReleaseLock();


Obviosly, I called thread initialization code in the main thread like
http://www.linuxjournal.com/article/3641
The problem with this code is that I hold the interpreter lock when calling
routine A or B so they cannot run
simultaneous and that is a requierement for me (as I explained before). It
executes some code of the routine I called but then it hungs.
If I release the lock before calling the Python code it doesn´t work neither
somhow.

Then I tried to create an interpreter for each thread. It is not desirable
but it's acceptable:

PyEval_AcquireLock();
PyThreadState* myThreadState = Py_NewInterpreter();
PyThreadState_Swap(myThreadState);
PyEval_ReleaseLock();

... Call Python code here

PyEval_AcquireLock();
PyThreadState_Swap(NULL);
Py_EndInterpreter(myThreadState);
PyEval_ReleaseLock();


But it doesn't
work. It seems that it requieres to take the lock to call Python code.
If I comment the PyEval_ReleaseLock line and PyEval_AcquireLock
pair it calls other threads before hunging.

I saw some threads talking about some similar situations and I saw no answer
to this.

This issue remains unsolved?


Thanks on advance,
Pablo

--
http://www.nektra.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070810/b5e109ae/attachment.html>


More information about the Python-list mailing list