Delivering data to python from a c-thread
"svein at seldal dot com" at STARDUST.DNSALIAS.com
Wed Nov 8 20:16:44 CET 2006
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.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!
More information about the Python-list