Exemplo n.º 1
0
from amqplib_thrift.factories import TAMQFactory
from amqplib import client_0_8 as amqp

# Set up sys.path to include thrift services
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'gen-py'))

# Thrift services
from tutorial import Calculator
from tutorial.ttypes import Work, Operation, InvalidOperation

# Set up connection
connection = amqp.Connection(host="10.0.0.100")
channel = connection.channel()

# Set up factory
factory = TAMQFactory(channel)

# Get the client
calculator = factory.get_client(Calculator.Client, 'calculator')

# Calculate something
calculator.ping()

print "4 + 5 = ", calculator.add(4, 5)

w = Work(num1=20, num2=10, op=Operation.MULTIPLY)
print "20 * 10 = ", calculator.calculate(1, w)

w = Work(num1=2, num2=0, op=Operation.DIVIDE)
try:
    result = calculator.calculate(2, w)
Exemplo n.º 2
0
        print "calculate(logid, w) called from client"
        try:
            return self.operations[w.op](w.num1, w.num2)
        except ZeroDivisionError, e:
            raise InvalidOperation(what=logid, why=e.args[0]) # e.message is deprecated!
    
    def zip(self):
        print "zip() called from client"
        
        import time
        time.sleep(10) # Long running process
        print "zip() finished"

processor = Calculator.Processor(CalculatorHandler())

factory = TAMQFactory(channel)
# Using the get_server method without a server_class argument is not
# reccomended as it blocks all processing for long lasting handler methods.
# The TForkingServer used here, does not work on windows.
# The thrift.server.TServer module offers other implementation of the TServe
# class which can be used here.
server = factory.get_server(processor, 'calculator', TForkingServer)

try:
    print "Starting server"
    server.serve()
except KeyboardInterrupt:
    print "Shutting down server"
    
    # Tear down the connection
    channel.close()