root / trunk / doc / core / examples / ampclient.py

Revision 19011, 0.9 kB (checked in by glyph, 3 years ago)

Fix Command inheritance in AMP, and improve documentation.

Command objects previously required all inherited commands to re-declare all
errors handled by their parent commands. That is now taken care of
automatically by the metaclass.

This change also improves documentation of callRemote and Command, since during
the process of fixing this bug it was discovered that not only were their
inheritance semantics undocumented, the rest of their semantics were mostly
undocumented too.

Author: glyph

Reviewers: dreid, exarkun

Fixes #2292

Line 
1 from twisted.internet import reactor, defer
2 from twisted.internet.protocol import ClientCreator
3 from twisted.protocols import amp
4 from ampserver import Sum, Divide
5
6
7 def doMath():
8     d1 = ClientCreator(reactor, amp.AMP).connectTCP(
9         '127.0.0.1', 1234).addCallback(
10             lambda p: p.callRemote(Sum, a=13, b=81)).addCallback(
11                 lambda result: result['total'])
12     def trapZero(result):
13         result.trap(ZeroDivisionError)
14         print "Divided by zero: returning INF"
15         return 1e1000
16     d2 = ClientCreator(reactor, amp.AMP).connectTCP(
17         '127.0.0.1', 1234).addCallback(
18             lambda p: p.callRemote(Divide, numerator=1234,
19                                    denominator=0)).addErrback(trapZero)
20     def done(result):
21         print 'Done with math:', result
22     defer.DeferredList([d1, d2]).addCallback(done)
23
24 if __name__ == '__main__':
25     doMath()
26     reactor.run()
Note: See TracBrowser for help on using the browser.