[Twisted-Python] Event notification and general RPC in Twisted...
![](https://secure.gravatar.com/avatar/57ff3776f01e761799763fa090e71b23.jpg?s=120&d=mm&r=g)
Hi all, I'm thinking about using Twisted to rewrite some communications in a Grid computing application. E.g.: [JobServer]1 ---------- *[Proxy]1 ----------- *[Agents] Agents get jobs, report status, results, etc through Proxy to JobServer. Agents are often distributed across a private network that has no external interface, hence the proxy, which is run on a machine between the private network and internet. Also, the proxy may do some caching/queuing of particular messages - especially where the number of agents behind it is large. What we have at present is a TCP socket server however we're starting to hit scalability issues and on top of that our future plans necessitate a notification framework. I've been digging around on twistedmatrix.com and in the book but so far am having trouble finding any guidance or the level of detail that tells me whether Twisted has what we need, some bits of which are: - we'd like to use a persistent stream/connection, at least between the JobServer and Proxy (traffic frequency will be reasonably high) - it needs to be interoperable with java (is there PB for java?) - each end of the connection should be able to register for and get notifications from the other (e.g. Agent gives a heartbeat, JobServer tells Agent to stop) - sometimes the Proxy might be behind a firewall and only able to connect out, we need to be able to use that connection to go back as above - we want to dynamically configure streaming connections between Proxies Hopefully that's enough context for you to point at modules and tell whether these are appropriate questions: Can I re-use a TCP stream for multiple XML-RPC or PB operations? If so, would it make sense to have the client (e.g. Proxy or Agent) initiate the connection and then make a rpc (e.g. "notify me of anything relevant") to the server which would eventually return when something relevant came along - triggering a callback in the client... wash, rinse, repeat? Sorry if I'm overlooking something that already answers this, it doesn't seem like something too unique to have not been asked before... Regards, -Blair -- In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite. - Paul Dirac
![](https://secure.gravatar.com/avatar/52cff2b66b9e291797f15f0c1b3491b8.jpg?s=120&d=mm&r=g)
On 2008.10.31 16:52:26 +1100, Blair Bethwaite wrote:
I'm thinking about using Twisted to rewrite some communications in a Grid computing application. E.g.:
[JobServer]1 ---------- *[Proxy]1 ----------- *[Agents]
Agents get jobs, report status, results, etc through Proxy to JobServer. Agents are often distributed across a private network that has no external interface, hence the proxy, which is run on a machine between the private network and internet. Also, the proxy may do some caching/queuing of particular messages - especially where the number of agents behind it is large.
What we have at present is a TCP socket server however we're starting to hit scalability issues and on top of that our future plans necessitate a notification framework. I've been digging around on twistedmatrix.com and in the book but so far am having trouble finding any guidance or the level of detail that tells me whether Twisted has what we need, some bits of which are:
- we'd like to use a persistent stream/connection, at least between the JobServer and Proxy (traffic frequency will be reasonably high) - it needs to be interoperable with java (is there PB for java?)
There is a Java version of PB. http://itamarst.org/software/twistedjava/ I've never used the Java version, so I can't say whether it works well. The Python version is excellent.
- each end of the connection should be able to register for and get notifications from the other (e.g. Agent gives a heartbeat, JobServer tells Agent to stop) - sometimes the Proxy might be behind a firewall and only able to connect out, we need to be able to use that connection to go back as above
PB allows both ends of the connection to send and receive at any time, over a single connection. I use it for a game where multiple clients connect via TCP to a server, and then the clients send messages to the server whenever they want, and the server sends messages to one or more clients whenever it wants (over the original connections initiated by the clients, so the clients don't need to open any holes in their firewalls), and everything just stinking works.
- we want to dynamically configure streaming connections between Proxies
Yes, you can add more connections at any time. You have to write your own simple routing code.
Hopefully that's enough context for you to point at modules and tell whether these are appropriate questions: Can I re-use a TCP stream for multiple XML-RPC or PB operations?
PB, yes. That's not the way XML-RPC is typically done. Clearly you could make a protocol that uses XML-RPC payload over a persistent connection. But you'd lose the ability to use arbitrary XML-RPC libraries unmodified, which is probably the main benefit of choosing XML-RPC. Also, XML-RPC is an inherently rigid request/response protocol and thus fails some of your other requirements. If the client and server are both asynchronously initiating requests over the same connection, you need additional information to distinguish a new request from a response to the other side's last request. And if you send multiple requests over the same connection without waiting for a response to the first, you need to send more information correlating requests with responses. All doable, but not XML-RPC anymore.
If so, would it make sense to have the client (e.g. Proxy or Agent) initiate the connection and then make a rpc (e.g. "notify me of anything relevant") to the server which would eventually return when something relevant came along - triggering a callback in the client... wash, rinse, repeat?
Yes, you make a remote call which returns a deferred. You attach a callback and an errback to the deferred. When the remote call finishes, either your callback or your errback fires, with your return value or exception information. I recommend writing something like a small chat system first, using the exact protocol you're considering, before tackling your real problem. If you can get Java clients and Python clients chatting through a chat proxy that can forward through other chat proxies, then you know you can make it work. When you write a little chatbot and run lots of instances in parallel and nothing chokes, then you know you can make it scale. If not for the Java requirement, I would say that Twisted is a good fit, and that you could use either PB or AMP, depending on whether you want to pass around complex types or simple ones. But if you need Java, I don't know. -- David Ripton dripton@ripton.net
![](https://secure.gravatar.com/avatar/d6328babd9f9a98ecc905e1ccac2495e.jpg?s=120&d=mm&r=g)
On 31 Oct, 04:14 pm, dripton@ripton.net wrote:
On 2008.10.31 16:52:26 +1100, Blair Bethwaite wrote:
If not for the Java requirement, I would say that Twisted is a good fit, and that you could use either PB or AMP, depending on whether you want to pass around complex types or simple ones. But if you need Java, I don't know.
AMP is specifically designed to be easy to implement and extend in different languages. I've already done a basic Java implementation, <http://bit.ly/3AGNhs>. It's not particularly well packaged or maintained, but that's mainly because nobody has voiced an interest in it yet; the code does work and has been used in production. If you're voicing such an interest then perhaps someone will step forward to clean it up :).
![](https://secure.gravatar.com/avatar/a317a8f80c2fc9e5df8470c599e89e2c.jpg?s=120&d=mm&r=g)
On Friday 31 October 2008 06:52:26 Blair Bethwaite wrote:
- we'd like to use a persistent stream/connection, at least between the JobServer and Proxy (traffic frequency will be reasonably high) - it needs to be interoperable with java (is there PB for java?) - sometimes the Proxy might be behind a firewall and only able to connect out, we need to be able to use that connection to go back as above
<ad> You can use Thrift [1] instead, which already supports Java and has a nifty patch which adds support for Twisted [2] </ad> I've tested it in an heterogeneous environment (Python, Java and Ruby) and it works fine. Cheers. 1 - http://incubator.apache.org/thrift/ 2 - https://issues.apache.org/jira/browse/THRIFT-148
![](https://secure.gravatar.com/avatar/57ff3776f01e761799763fa090e71b23.jpg?s=120&d=mm&r=g)
David, thanks for your response, it's great when people take the time to read a lengthy question and then respond with some insight! David Ripton wrote:
PB allows both ends of the connection to send and receive at any time, over a single connection. I use it for a game where multiple clients connect via TCP to a server, and then the clients send messages to the server whenever they want, and the server sends messages to one or more clients whenever it wants (over the original connections initiated by the clients, so the clients don't need to open any holes in their firewalls), and everything just stinking works.
I ended up sort of answering this one for myself by just playing with a basic chat server example and sniffing packets - only saw SYN,SYN-ACK,ACK once for each client. Though it would be nice if the documentation gave this level of detail, presumably this is something that belongs with the different transports.
That's not the way XML-RPC is typically done. Clearly you could make a protocol that uses XML-RPC payload over a persistent connection. But you'd lose the ability to use arbitrary XML-RPC libraries unmodified, which is probably the main benefit of choosing XML-RPC.
Duh, good point! Guess I was kinda trying to map our requirements onto an example that looked easy (from the book).
If not for the Java requirement, I would say that Twisted is a good fit, and that you could use either PB or AMP, depending on whether you want to pass around complex types or simple ones. But if you need Java, I don't know.
Guess I'll have to take a closer look at the Java implementation. Not sure why I didn't stumble across AMP before but looks promising... Thanks, -Blair -- In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it's the exact opposite. - Paul Dirac
![](https://secure.gravatar.com/avatar/d6eb730e1b781945e8bb86cad1894ab9.jpg?s=120&d=mm&r=g)
I'm going to chime in here with a pretty general comment... On Nov 5, 2008, at 1:18 AM, Blair Bethwaite wrote:
Guess I'll have to take a closer look at the Java implementation. Not sure why I didn't stumble across AMP before but looks promising...
I can probably guess why you didn't stumble across AMP, because it took me a long time to discover it as well. The problem is that ti isn't really discussed anywhere in the Twisted documentation (other than in the API description). It is, I think, one of the better kept secrets in the Twisted arsenal. The only exception is on this list, where it comes up frequently as the solution to a lot of problems (though without much detail). I wonder if there wouldn't be some benefit to adding a section in the core documentation near the section on the PB... I would write some myself, but I didn't find AMP in time and wrote the apps I currently work with using PB or custom protocols and I just haven't had the time to learn AMP and work with it...
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Wed, 5 Nov 2008 10:52:46 -0500, Black <python@blackslens.com> wrote:
I'm going to chime in here with a pretty general comment...
On Nov 5, 2008, at 1:18 AM, Blair Bethwaite wrote:
Guess I'll have to take a closer look at the Java implementation. Not sure why I didn't stumble across AMP before but looks promising... I can probably guess why you didn't stumble across AMP, because it took me a long time to discover it as well. The problem is that ti isn't really discussed anywhere in the Twisted documentation (other than in the API description). It is, I think, one of the better kept secrets in the Twisted arsenal.
One reason for that is that AMP is about two years old; PB is at least 7, probably a little bit older. Of course, it'd be great to have some howto-style documentation for AMP. Jean-Paul
![](https://secure.gravatar.com/avatar/42148ea9c50179d54c77987a998af7a0.jpg?s=120&d=mm&r=g)
I also highly recommend Foolscap (http://foolscap.lothar.com/trac), which was designed as a next generation PB-like RPC protocol. Some advantages of Foolscap: * Security is built-in and easy to use. The security model of Foolscap is based on the object capability model and is really nice. If you need security (encryption+authentication) Foolscap is the way to go. PB can be made secure, but it is not straighforward. * Much better support for streaming large objects than PB or AMP has. We have used PB extensively as well though and if you don't need security or the ability to handle large objects, it it great. Bran On Wed, Nov 5, 2008 at 8:19 AM, Jean-Paul Calderone <exarkun@divmod.com> wrote:
On Wed, 5 Nov 2008 10:52:46 -0500, Black <python@blackslens.com> wrote:
I'm going to chime in here with a pretty general comment...
On Nov 5, 2008, at 1:18 AM, Blair Bethwaite wrote:
Guess I'll have to take a closer look at the Java implementation. Not sure why I didn't stumble across AMP before but looks promising...
I can probably guess why you didn't stumble across AMP, because it took me a long time to discover it as well. The problem is that ti isn't really discussed anywhere in the Twisted documentation (other than in the API description). It is, I think, one of the better kept secrets in the Twisted arsenal.
One reason for that is that AMP is about two years old; PB is at least 7, probably a little bit older.
Of course, it'd be great to have some howto-style documentation for AMP.
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
![](https://secure.gravatar.com/avatar/826694d326649b5e681474e8e2116dc1.jpg?s=120&d=mm&r=g)
On Wed, Nov 5, 2008 at 10:18 AM, Brian Granger <ellisonbg.net@gmail.com> wrote:
I also highly recommend Foolscap (http://foolscap.lothar.com/trac), which was designed as a next generation PB-like RPC protocol. Some advantages of Foolscap:
Is Foolscap actually going to be pulled into the actual Twisted tree any time soon? I've wanted to try it out for some of our projects, but one of our project goals is minimizing external dependencies... ~ Nathan
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Fri, 7 Nov 2008 10:08:18 -0700, Nathan <nathan.stocks@gmail.com> wrote:
On Wed, Nov 5, 2008 at 10:18 AM, Brian Granger <ellisonbg.net@gmail.com> wrote:
I also highly recommend Foolscap (http://foolscap.lothar.com/trac), which was designed as a next generation PB-like RPC protocol. Some advantages of Foolscap:
Is Foolscap actually going to be pulled into the actual Twisted tree any time soon? I've wanted to try it out for some of our projects, but one of our project goals is minimizing external dependencies...
As yet, there has been no discussion about doing this. That probably precludes it from happning "soon", at least. Jean-Paul
![](https://secure.gravatar.com/avatar/826694d326649b5e681474e8e2116dc1.jpg?s=120&d=mm&r=g)
On Fri, Nov 7, 2008 at 10:22 AM, Jean-Paul Calderone <exarkun@divmod.com> wrote:
On Fri, 7 Nov 2008 10:08:18 -0700, Nathan <nathan.stocks@gmail.com> wrote:
Is Foolscap actually going to be pulled into the actual Twisted tree any time soon? I've wanted to try it out for some of our projects, but one of our project goals is minimizing external dependencies...
As yet, there has been no discussion about doing this. That probably precludes it from happning "soon", at least.
Jean-Paul
Ok, that's good to know. I may just have to buckle-down and accept one more dependency then, because I want to try out that large-object support ... ~ Nathan
![](https://secure.gravatar.com/avatar/0f15c04b6acde258bd27586371ae94b1.jpg?s=120&d=mm&r=g)
As yet, there has been no discussion about doing this. That probably precludes it from happning "soon", at least.
Yeah, I'm not pushing to get Foolscap into the core any time soon. The release cycles are just too different. cheers, -Brian BTW: foolscap-0.3.2 is the latest version, at http://foolscap.lothar.com . I keep forgetting to email the release announcements.
participants (9)
-
Black
-
Blair Bethwaite
-
Brian Granger
-
Brian Warner
-
David Ripton
-
Esteve Fernandez
-
glyph@divmod.com
-
Jean-Paul Calderone
-
Nathan