Calling python interpreter from a thread created in C

Greg Chapman glc at well.com
Thu Nov 4 09:42:00 EST 2004


On Tue, 02 Nov 2004 11:50:33 GMT, Jarppe <jarppe_remove_this_ at gmail.com> wrote:

>Hello,
>
>What should I do to be able to call Pyhton interpreter from a posix 
>thread that is created in C?
>
>I have a C-library that creates a thread and then makes callbacks to 
>user supplied function from that threads context. I'm writing a wrapper 
>for this library and I need to call python interpreter from that callback.
>
>I'm using Python 2.3 in Linux.
>
>Any help appreciated.

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).

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.)

---
Greg Chapman





More information about the Python-list mailing list