Delivering data to python from a c-thread

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

    #1

    Delivering data to python from a c-thread

    Hi,

    I have a C-application that calls a Python function main(). This
    function will loop forever and not return until the entire application
    is about to terminate.

    In a parallel C-thread, some data must be regularly delivered to the
    running python application. My initial plan was to call a py function
    deliver() from this thread, however I constantly run into troubles. I
    keep getting a "Fatal Python error: ceval: tstate mix-up" error from
    python, even when I handle the GIL with PyGILState_Ensu re().

    I fail to use PyEval_AcquireL ock() prior to python call either, as the
    main app would then constantly keep this lock when its running
    permanently in python.

    Basically I would the thread to stop the execution of the main py app,
    call the message function deliver(). When the function returns from
    python, resume the execution of the main pyapp.


    Regards,
    Svein Seldal



  • Steve Holden

    #2
    Re: Delivering data to python from a c-thread

    Svein Seldal wrote:
    Hi,
    >
    I have a C-application that calls a Python function main(). This
    function will loop forever and not return until the entire application
    is about to terminate.
    >
    In a parallel C-thread, some data must be regularly delivered to the
    running python application. My initial plan was to call a py function
    deliver() from this thread, however I constantly run into troubles. I
    keep getting a "Fatal Python error: ceval: tstate mix-up" error from
    python, even when I handle the GIL with PyGILState_Ensu re().
    >
    I fail to use PyEval_AcquireL ock() prior to python call either, as the
    main app would then constantly keep this lock when its running
    permanently in python.
    >
    Basically I would the thread to stop the execution of the main py app,
    call the message function deliver(). When the function returns from
    python, resume the execution of the main pyapp.
    >
    Could you have the Python code create a second Python thread and have it
    call back into the C code to collect any waiting data?

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden

    Comment

    • Svein Seldal

      #3
      Re: Delivering data to python from a c-thread

      Steve Holden wrote:
      Could you have the Python code create a second Python thread and have it
      call back into the C code to collect any waiting data?
      Well yeah, in principle. However one would need some synchronization
      mechanisms anyway. The C data source is generating asynch. messages to
      deliver to python and thus the py thread must be ready to wait for it.
      It will add another thread in the total application (cuz' I cant remove
      the extra C thread since it has other important tasks), but I'll give it
      a shot at least!

      Regards
      Svein Seldal

      Comment

      • Steve Holden

        #4
        Re: Delivering data to python from a c-thread

        Svein Seldal wrote:
        Steve Holden wrote:
        >
        >Could you have the Python code create a second Python thread and have it
        >call back into the C code to collect any waiting data?
        >
        Well yeah, in principle. However one would need some synchronization
        mechanisms anyway. The C data source is generating asynch. messages to
        deliver to python and thus the py thread must be ready to wait for it.
        It will add another thread in the total application (cuz' I cant remove
        the extra C thread since it has other important tasks), but I'll give it
        a shot at least!
        >
        OK. I was just thinking that, with Python threads, communication using
        Queue.Queue is thread-safe and will handle the GIL, so that way you only
        have the problem of how to synchronize your C code when it receives the
        callback from the Python thread.

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd http://www.holdenweb.com
        Skype: holdenweb http://holdenweb.blogspot.com
        Recent Ramblings http://del.icio.us/steve.holden

        Comment

        • Svein Seldal

          #5
          Re: Delivering data to python from a c-thread

          Steve Holden wrote:
          OK. I was just thinking that, with Python threads, communication using
          Queue.Queue is thread-safe and will handle the GIL, so that way you only
          have the problem of how to synchronize your C code when it receives the
          callback from the Python thread.
          The python internal thread-safe problem is under control. I'm using
          twisted, so it already provide me with a thread-synchronization
          mechanism (the reactor.callFro mThread() ).

          The issue is rather the C sync-ing. I'm kind of new to multithreaded
          (pthreads) programming, so I'm not quite sure what exists of
          functionality and functions. So this is how I think of doing it:

          Setup a mailbox or similar where the producing thread will deliver its
          message. This call to the mailbox should block until the consumer thread
          has received the message. My py thread will repeatedly call my
          C-handler. This C handler will wait (forever) for incoming messages from
          the producer. This should work, right? I mean, this could even be
          implemented with flags/semaphores and let the data be transferred over
          ordinary common global variables.

          Francly, I think the best solution would be to be able to call py from
          two independent c-treads!

          Regads,
          Svein Seldal

          Comment

          • Svein Seldal

            #6
            Re: Delivering data to python from a c-thread

            Steve Holden wrote:
            OK. I was just thinking that, with Python threads, communication using
            Queue.Queue is thread-safe and will handle the GIL, so that way you only
            have the problem of how to synchronize your C code when it receives the
            callback from the Python thread.
            The python internal thread-safe problem is under control. I'm using
            twisted, so it already provide me with a thread-synchronization
            mechanism (the reactor.callFro mThread() ).

            The issue is rather the C sync-ing. I'm kind of new to multithreaded
            (pthreads) programming, so I'm not quite sure what exists of
            functionality and functions. So this is how I think of doing it:

            Setup a mailbox or similar where the producing thread will deliver its
            message. This call to the mailbox should block until the consumer thread
            has received the message. My py thread will repeatedly call my
            C-handler. This C handler will wait (forever) for incoming messages from
            the producer. This should work, right? I mean, this could even be
            implemented with flags/semaphores and let the data be transferred over
            ordinary common global variables.

            Francly, I think the best solution would be to be able to call py from
            two independent c-treads!

            Regads,
            Svein Seldal

            Comment

            Working...