Discussion:
[Twisted-Python] Multiple reactors, connecting to self, or other solution?
Glyph Lefkowitz
2015-11-18 08:55:42 UTC
Permalink
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
Pantelis Theodosiou
2015-11-18 09:21:03 UTC
Permalink
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
Glyph, the linked page:
https://twistedmatrix.com/documents/15.4.0/core/howto/servers.html has
endpoints example but the "Putting it All Together" still uses:

reactor.listenTCP(8123, ChatFactory())


Wouldn't it be better if that was replaced, too? Or maybe both versions be
visible on the same webpage? (one with listenTCP and one with endpoints)?

Pantelis
Glyph Lefkowitz
2015-11-18 09:43:21 UTC
Permalink
Wouldn't it be better if that was replaced, too? Or maybe both versions be visible on the same webpage? (one with listenTCP and one with endpoints)?
Absolutely! In this high-level explanation of how to write a server, really only the endpoints example should be shown. Would you mind filing a ticket (and perhaps also sending a patch? :))

-glyph
Oon-Ee Ng
2015-11-20 05:19:38 UTC
Permalink
On Wed, Nov 18, 2015 at 4:55 PM, Glyph Lefkowitz
Post by Glyph Lefkowitz
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.
Glyph Lefkowitz
2015-11-20 05:22:35 UTC
Permalink
Post by Oon-Ee Ng
On Wed, Nov 18, 2015 at 4:55 PM, Glyph Lefkowitz
Post by Glyph Lefkowitz
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
Oon-Ee Ng
2015-11-20 08:32:32 UTC
Permalink
On Fri, Nov 20, 2015 at 1:22 PM, Glyph Lefkowitz
Post by Glyph Lefkowitz
Post by Oon-Ee Ng
On Wed, Nov 18, 2015 at 4:55 PM, Glyph Lefkowitz
Post by Glyph Lefkowitz
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
I know better than to argue this point. My app as it currently stands
will only ever be used on local networks (doubt the way I'm designing
it would handle high latencies well), and I'm really using twisted as
a more convenient/performant socket with AMP on top.

That being said, using endpoints is easy enough (it seems), and I'm
going to give it a go once I'm done crafting all these messages (based
on the earlier response in this thread I'm totally cutting blinker out
of the app and replacing all my signals).

Continue reading on narkive:
Loading...