[Twisted-Python] How to stuff new events into the core event loop?
Ames Andreas (MPA/DF)
Andreas.Ames at tenovis.com
Fri Oct 15 11:47:51 EDT 2004
I'm pretty new to twisted activity (just some doc and code reading so
far). So please be forbearing if the answers to the following
questions are obvious.
I'm wrapping some C library which implements some networked client to
use it from python, i.e. from a twisted app. Within the guts of this
lib there is a thread created (i.e. it is normally never seen by
python). This thread manages some heartbeat mechanism. I, as a user
of this API, can register a callback with the lib. This callback is
called _within the context of this underground thread_, when the
connection to the server is lost (as recognised through the
heartbeat). Now my first question is, how you would stuff that event
(loss of server connection) into the core event loop?
One possibility that I see would be to let my registered C-callback
signal some sort of a synchronisation item (like mutex or so) whenever
the connection to the server is lost. Then I'm using a threaded
deferred to block on this synchronisation item, triggering the
deferred's callback mechanism when the item gets signaled. But this
approach seems very wasteful to me, as I'm using a deferred and a
thread for an event that will never happen (my own application is a
long running process either).
Actually I'm wrapping not only one but two such C libraries. Both
implement protocols that are not (yet?) available for python/twisted
and that are not trivial (just to illustrate that there are not many
alternatives to using these C libs). I'm planning to use both of them
through twisted's thread pool mechanism. The only thing is that I
would like to have fine grained control over the number of threads
used for each of them. Is it possible to create custom thread pools
besides the default one? If so, is there anything preventing me from
creating a thread pool with only one thread?
More information about the Twisted-Python