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.
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. Usage
1. Define data structure and service interface
using IDL(interface description language)
2. Generate language binding library:
thrift -gen py my.thrift
3. 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. Example
• Scenario:
• We have a prepared machine learning model,
and we'd like to load the model in advance as
a server to provide prediction service.
Thrift http://thrift.apache.org/ 6
7. Step 1. Define data structure and
service: classifier.thrift
typedef list<double> Feature
service Classifier{
i64 predict(1: Feature feature);
}
Thrift http://thrift.apache.org/ 7
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. Client (1/2)
# Make socket
transport = TSocket.TSocket('localhost', 9090)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = Classifier.Client(protocol)
# Connect!
transport.open()
Thrift http://thrift.apache.org/ 10
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. 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