| 1 |
from ampserver import Divide, Sum |
| 2 |
|
| 3 |
from twisted.internet import defer, reactor |
| 4 |
from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol |
| 5 |
from twisted.protocols.amp import AMP |
| 6 |
|
| 7 |
|
| 8 |
def doMath(): |
| 9 |
destination = TCP4ClientEndpoint(reactor, "127.0.0.1", 1234) |
| 10 |
sumDeferred = connectProtocol(destination, AMP()) |
| 11 |
|
| 12 |
def connected(ampProto): |
| 13 |
return ampProto.callRemote(Sum, a=13, b=81) |
| 14 |
|
| 15 |
sumDeferred.addCallback(connected) |
| 16 |
|
| 17 |
def summed(result): |
| 18 |
return result["total"] |
| 19 |
|
| 20 |
sumDeferred.addCallback(summed) |
| 21 |
|
| 22 |
divideDeferred = connectProtocol(destination, AMP()) |
| 23 |
|
| 24 |
def connected(ampProto): |
| 25 |
return ampProto.callRemote(Divide, numerator=1234, denominator=0) |
| 26 |
|
| 27 |
divideDeferred.addCallback(connected) |
| 28 |
|
| 29 |
def trapZero(result): |
| 30 |
result.trap(ZeroDivisionError) |
| 31 |
print("Divided by zero: returning INF") |
| 32 |
return 1e1000 |
| 33 |
|
| 34 |
divideDeferred.addErrback(trapZero) |
| 35 |
|
| 36 |
def done(result): |
| 37 |
print("Done with math:", result) |
| 38 |
reactor.stop() |
| 39 |
|
| 40 |
defer.DeferredList([sumDeferred, divideDeferred]).addCallback(done) |
| 41 |
|
| 42 |
|
| 43 |
if __name__ == "__main__": |
| 44 |
doMath() |
| 45 |
reactor.run() |