Calling python interpreter from a thread created in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jarppe

    #1

    Calling python interpreter from a thread created in C

    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
  • Greg Chapman

    #2
    Re: Calling python interpreter from a thread created in C

    On Tue, 02 Nov 2004 11:50:33 GMT, Jarppe <jarppe_remove_ this_@gmail.com > wrote:
    [color=blue]
    >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.[/color]

    First, you need to ensure that Python is ready for threading. From the
    application's main thread, call PyEval_InitThre ads (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_Ensu re before accessing any
    part of the Python API. When you're done working with python, call
    PyGILState_Rele ase. See the documentation and example here:



    (Note that before the example of using PyGILState_Ensu re, 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


    Comment

    • Jarppe

      #3
      Re: Calling python interpreter from a thread created in C

      Hi,

      Greg Chapman wrote:[color=blue]
      > First, you need to ensure that Python is ready for threading. From the
      > application's main thread, call PyEval_InitThre ads (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).[/color]

      That's it!!! I didn't realize that and I just keep getting SIGSEGV no
      matter what I did. I added call to PyEval_InitThre ads() and it started
      to work immediately! Thanks!!!
      [color=blue]
      > Then, in your thread's callback, call PyGILState_Ensu re before accessing any
      > part of the Python API. When you're done working with python, call
      > PyGILState_Rele ase. See the documentation and example here:
      >
      > http://www.python.org/dev/doc/devel/api/threads.html
      >
      > (Note that before the example of using PyGILState_Ensu re,
      > 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.)[/color]

      Oh, that's interesting! I used PyEval_AcquireT hread() and
      PyEval_ReleaseT hread(). Now I changed to
      PyGILState_Ensu re()/PyGILState_Rele ase(), 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

      Comment

      Working...