[Twisted-Python] Twisted communication with embedded C thread
tom.cocagne at gmail.com
Thu Jul 7 16:18:35 EDT 2005
Thanks for the quick response guys.
A bit more of a description of what I'm working on may help.
Basically, I'm working on building a coordination library for use in
programs taking part in a distributed simulation. The simulations
themselves are written primarily in C++, run on a bunch of different
platforms, and are often optomized to use the specific hardware
they're running on top of (C-threads locked to specific CPUs, arcane
compiler flags, the whole shebang). The Python/Twisted side handles
all the high-level coordination stuff while the C++ code focuses on
As the library is supposed to be as generic as possible, I can't
make too many assumptions about how threading will be handled in any
particular application. So to keep things simple I'm creating a single
thread that is dedicated to running the Python interpreter and
allowing only that thread to touch the interpreter.
I looked into the code for reactor.callFromThread that y'all
mentioned and it looks like the implementation for it exactly answers
the question I've been trying to ask. On posix systems the reactor's
'waker' is implemented as a pair of pipes and on Win32 systems it's
implemented as a pair of sockets. So it appears that my original
approach will work except that on Win32 systems I'll just have to use
a socket rather than a pipe.
Thanks for the help,
BTW Itamar, I took a look at the SmartFrog/Coil documentation you
sent out yesterday and it looks very promising. Do you have any sort
of roadmap/timeline for Coil?
On 7/7/05, Itamar Shtull-Trauring <itamar at itamarst.org> wrote:
> On Thu, 2005-07-07 at 13:48 -0400, Tom Cocagne wrote:
> > I was wondering if anyone has suggestions for a good way to go
> > about setting up a message-passing communication channel between one
> > or more C threads and a single thread dedicated to running
> > Python/Twisted. I already have functional Python bindings accessing
> > the contents of the messages (basically C structures) but need some
> > way to exchange the messages between the C threads and the Python
> > thread.
> Any reason you're using threads? You can run Twisted directly on the
> glib event loop, so your whole program could run on the same loop.
> In any case, Switching data from Twisted to C is just like passing data
> from any Python program to C code: call a C extension function from
> Python that calls into C code, and for reverse call a Python function
> from C code. Thread safety is mostly orthogonal, so you can probably use
> the standard Twisted reactor.callFromThread to have code run in the
> Twisted thread.
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
More information about the Twisted-Python