[Twisted-Python] RPC design questions

Tobias Oberstein tobias.oberstein at tavendo.de
Tue Aug 23 10:37:34 EDT 2011


> > self.call(<procedure>, <arg>)
> > self.call("square", 6).addCallback(self.call,
> > "sqrt").addCallback(self.show)
> 
> The easiest way to do this is probably:
> 
> from functools import partial
> self.call("square", 6).addCallback(partial(self.call,
> "sqrt")).addCallback(self.show)

Thanks alot! Did not knew about partial .. I checked the solution you proposed: works.

It rectifies the arg order, but introduces more boilerplate to write (and making
usage convenient is my goal).

===

In the meantime, I've come up with 3 others options:

No chaining (no problem here):

      self.call("add", 23*23, 5).addCallback(self.show)

Chaining Option 1:

      self.call("square", 23).addCallback(self.rcall, "add", 5).addCallback(self.show)

Chaining Option 2:

      self.call("square", 23).addCallback(lambda res: self.call("add", res, 5)).addCallback(self.show)

Chaining Option 3:

      self.call("square", 23).call("add", 5).addCallback(self.show)


Option 1
=======

Makes use of rcall() vs call()  where rcall() does the reordering of arguments

   def rcall(self, *args):
      a = []
      a.append(args[1]) # procedure ID
      if args[0]:
         a.append(args[0]) # result from previous deferred
         a.extend(args[2:]) # new args
      return self.call(*a)

Pro:	terse, can handle callback&errback
Con:	user needs to remember to use rcall() not call()


Option 2
======
Similar to yours .. just using lambda.

Pro:	standard Python (as yours), can handle callbacks&errbacks
Con:	verbose


Option 3
=======

Makes use of

class AutobahnDeferred(Deferred):
   def call(self, *args):
      return self.addCallback(self.protocol.rcall, *args)

Pro:	most terse
Con:	only supports single callback no errback

====

Currently my thinking is: why not provide all variants?

Anything why I shouldn't do?


> mithrandi, i Ainil en-Balandor, a faer Ambar

ok. nice;) but what does it mean?


> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python



More information about the Twisted-Python mailing list