Delivering data to python from a c-thread

S

Svein Seldal

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

I fail to use PyEval_AcquireLock() 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
 
S

Steve Holden

Svein said:
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_Ensure().

I fail to use PyEval_AcquireLock() 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
 
S

Svein Seldal

Steve said:
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
 
S

Steve Holden

Svein said:
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
 
S

Svein Seldal

Steve said:
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.callFromThread() ).

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
 
S

Svein Seldal

Steve said:
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.callFromThread() ).

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
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top