[Twisted-Python] Getting a synchronous interface to a twisted reactor

Phil Mayers p.mayers at imperial.ac.uk
Thu Apr 22 11:00:12 EDT 2010

On 22/04/10 15:50, Andrey Fedorov wrote:
> On Thu, Apr 22, 2010 at 3:21 AM, Phil Mayers <p.mayers at imperial.ac.uk
> <mailto:p.mayers at imperial.ac.uk>> wrote:
>     If that's the case, then the answer is "sort of" but I think you
>     might have misunderstood Twisted a bit. The entire point is to
>     have non-blocking APIs, not to want a blocking API.
> That sounds like might be the case case. Wwhat I'm looking for, then, is
> a way to use the IRCClient protocol outside of Twisted - in a way where
> I can use it like:
>     connection = # something using IRCClient
>     msg = connection.privmsg() # blocks
>     assert msg == "andreyf: hello there, lovely IRC users!"
> Are there any projects along those lines?

Not that I'm aware of. I'm pretty certain the Twisted protocol 
implementations all require the reactor and thus need to be run in an 
async manner.

There are various hacky ways you could accomplish this; the obvious one 
that springs to mind is running the reactor in a thread. There are 
various caveats to doing this, the main one being you can't call any 
Twisted functions outside the reactor thread except "callFromThread" so 
you'd probably want to write some kind of wrapper; and it's a bit 
counter to the whole ethos.

Given you said:

"""The main reason for the question, though, is just curiosity from 
playing with and learning the Twisted API"""

...I would recommend just trying to use the API in the Twisted "way". 
You can use the aforementioned @inlineCallbacks decorator & generator 
syntax if you prefer writing your code that way, as opposed to 
traditional deferred callback chaining.

More information about the Twisted-Python mailing list