Apache thrift-RPC service cross languages

2,476 views

Published on

This slides illustrate how to use Apache Thrift for building RPC service and provide demo example code in Python. The example scenario is: we have a prepared machine learning model, and we'd like to load the model in advance as a server for providing prediction service.

Published in: Technology
  • Be the first to comment

Apache thrift-RPC service cross languages

  1. 1. Apache Thrift:RPC service cross languages 2012/11/04 Jimmy Lai r97922028 [at] ntu.edu.tw
  2. 2. Thrift Network Stack Thrift http://thrift.apache.org/ 2
  3. 3. Languages Supported• C++ • Java• C# • OCaml• Cocoa • Perl• D • PHP• Delphi • Python• Erlang • Ruby• Haskell • Smalltalk Thrift http://thrift.apache.org/ 3
  4. 4. Interface Description Language (IDL) Data structure bool | byte | i16 | i32 | i64 | double | string | binary | slist map set list• http://thrift.apache.org/docs/idl/ Thrift http://thrift.apache.org/ 4
  5. 5. Usage1. Define data structure and service interface using IDL(interface description language)2. Generate language binding library: thrift -gen py my.thrift3. Write Server and Client using the generated library. You may refer the template file in thrift/tutorial directory. Thrift http://thrift.apache.org/ 5
  6. 6. Example• Scenario:• We have a prepared machine learning model, and wed like to load the model in advance as a server to provide prediction service. Thrift http://thrift.apache.org/ 6
  7. 7. Step 1. Define data structure and service: classifier.thrifttypedef list<double> Featureservice Classifier{ i64 predict(1: Feature feature);} Thrift http://thrift.apache.org/ 7
  8. 8. Step 2. Generate binding library• thrift --gen py classifier.thrift• Code binding library in gen-py dir Thrift http://thrift.apache.org/ 8
  9. 9. Step 3. Write Service and Client• Server class ClassifierHandler(object): def __init__(self): self.model = pkl.load(open(svm_model.pkl, rb)) print loading model... def predict(self, feature): return self.model.predict(feature)[0] Thrift http://thrift.apache.org/ 9
  10. 10. Client (1/2)# Make sockettransport = TSocket.TSocket(localhost, 9090)# Buffering is critical. Raw sockets are very slowtransport = TTransport.TBufferedTransport(transport)# Wrap in a protocolprotocol = TBinaryProtocol.TBinaryProtocol(transport)# Create a client to use the protocol encoderclient = Classifier.Client(protocol)# Connect!transport.open() Thrift http://thrift.apache.org/ 10
  11. 11. Client (2/2)digits = datasets.load_digits()data = zip(digits.data, digits.target)random.shuffle(data)for i in range(10): result = client.predict(data[i][0]) answer = data[i][1] if result == answer: print Correct! else: print Wrong: answer is %d, predicted as %d % (answer,result)# Close!transport.close() Thrift http://thrift.apache.org/ 11
  12. 12. Live Demo• See thrift_demo directory in https://bitbucket.org/noahsark/slideshare/ noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonServer.py loading model... Starting the server... noahsark@localhost:~/workspace/thrift/gen-py$ ./PythonClient.py Correct! Correct! Correct! Correct! Correct! Correct! Correct! Wrong: answer is 8, predicted as 1 Correct! Correct! Thrift http://thrift.apache.org/ 12

×