Calling python interpreter from a thread created in C

J

Jarppe

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.

--

-jarppe

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

Greg Chapman

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

Jarppe

Hi,

Greg said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,210
Messages
2,571,091
Members
47,692
Latest member
RolandRose

Latest Threads

Top