Calling python interpreter from a thread created in C

Jarppe jarppe_remove_this_ at gmail.com
Fri Nov 5 08:28:16 EST 2004


Hi,

Greg Chapman wrote:
> First, you need to ensure that Python is ready for threading.  From the
> application's main thread, call PyEval_InitThreads (if this has already been
> called, it simply returns.  But if this is the first call, it assumes it is
> being called on the application's main thread, so you don't want to call this on
> your callback thread).

That's it!!! I didn't realize that and I just keep getting SIGSEGV no 
matter what I did. I added call to PyEval_InitThreads() and it started 
to work immediately! Thanks!!!

> Then, in your thread's callback, call PyGILState_Ensure before accessing any
> part of the Python API.  When you're done working with python, call
> PyGILState_Release.  See the documentation and example here:
 >
 > http://www.python.org/dev/doc/devel/api/threads.html
 >
 > (Note that before the example of using PyGILState_Ensure,
 > the text refers to
 > needing an interpreter state.  This text is left over
 > from versions before
 > PyGILState was introduced.  Just do what the example shows
 > and you shouldn't have a problem.)

Oh, that's interesting! I used PyEval_AcquireThread() and 
PyEval_ReleaseThread(). Now I changed to 
PyGILState_Ensure()/PyGILState_Release(), and now the code looks much 
simpler. And it works :)

Thanks a lot!!!

-- 

-jarppe

   If you have the right attitude, interesting problems will find you.
       -- Eric Steven Raymond



More information about the Python-list mailing list