Need some help with Python/C api and threading

Steve Menard steve.menard at videotron.ca
Wed Jun 16 15:30:43 EDT 2004


Here is my problem.

I have this library thats hosts another language within python, and 
allows that language to call back INTO python.

All is good as long as the other languages calls back on the same 
thread. If the callback arrives on a different thread, all hell break 
loose and the program dies horribly.

looking at the C api documentation, I came upon the following block of 
code :

     PyThreadState *tstate;
     PyObject *result;

     /* interp is your reference to an interpreter object. */
     tstate = PyThreadState_New(interp);
     PyEval_AcquireThread(tstate);

     /* Perform Python actions here.  */
     result = CallSomeFunction();
     /* evaluate result */

     /* Release the thread. No Python API allowed beyond this point. */
     PyEval_ReleaseThread(tstate);

     /* You can either delete the thread state, or save it
        until you need it the next time. */
     PyThreadState_Delete(tstate);


Which would seem to be what I need. However, I have no idea how to get 
at that interp pointer. I tried the following :

		PyInterpreterState* interp = PyInterpreterState_New();
		PyThreadState *tstate = PyThreadState_New(interp);
		PyEval_AcquireThread(tstate);

but then it crashes on the second line ...

Anybody ever done this? As a side note, the hosted language can start an 
arbitrary number of threads ...

Steve



More information about the Python-list mailing list