[Twisted-Python] Multiple reactors, connecting to self, or other solution?

Glyph Lefkowitz glyph at twistedmatrix.com
Thu Nov 19 22:22:35 MST 2015


> On Nov 19, 2015, at 9:19 PM, Oon-Ee Ng <ngoonee.talk at gmail.com> wrote:
> 
> On Wed, Nov 18, 2015 at 4:55 PM, Glyph Lefkowitz
> <glyph at twistedmatrix.com> wrote:
>> 
>>> On Nov 15, 2015, at 9:54 AM, Itamar Turner-Trauring <itamar at itamarst.org> wrote:
>>> 
>>> On 11/15/2015 10:19 AM, Oon-Ee Ng wrote:
>>>> Based on my reading/searching, multiple reactors in the same process
>>>> (even in multiple threads) is pretty much a no-go because
>>>> twisted.internet.reactor is a global singleton.
>>>> 
>>>> I'm also unable to find any information about connecting to self (for
>>>> example, to send messages from one reactor to itself).
>>>> 
>>>> 
>>> 
>>> You can just have a single reactor. E.g. if you do a listenTCP (e.g. on port 8080) on the reactor you can in the same process do a connectTCP to localhost in the same process on the same reactor; just connect to '127.0.0.1' or 'localhost' on port 8080.
>> 
>> But of course you'd use <https://twistedmatrix.com/documents/15.4.0/core/howto/endpoints.html>, not 'listenTCP' and 'connectTCP' directly, right? :)
>> 
>> -glyph
> 
> 
> But, but... I LIKE listen/connectTCP.... fits better with how I think.

If you use connectTCP/listenTCP, you miss out on important functionality.  For example, listenTCP can't do encryption, which means it's unsuitable for use on the modern internet, unless your protocol calls startTLS right away.

Also you don't get stuff like https://txtorcon.readthedocs.org for free.

So, connectTCP/listenTCP are low-level APIs that should really only be used for *implementing* an endpoint, not used directly by applications.  If this does not fit with how you think then you need to change how you think :).

-glyph





More information about the Twisted-Python mailing list