Need some help with Python/C api and threading

Ronald Oussoren ronaldoussoren at mac.com
Fri Jun 18 10:19:05 EDT 2004


If you're using Python 2.3 I'd use the API described in PEP 311:

	http://www.python.org/peps/pep-0311.html


Ronald

On 16-jun-04, at 21:30, Steve Menard wrote:

> 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
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>
>
--
X|support bv            http://www.xsupport.nl/
T:  +31 610271479       F:  +31 204416173





More information about the Python-list mailing list