thrift_client.py
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Michał Lenart
# This is THRIFT client for Multiservice platform.
# It is available on the same license as the Multiservice itself.
#
import sys
import re
import jsonpickle
import json
import time
import StringIO
from optparse import OptionParser
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from multiservice.facade import Multiservice
from multiservice.facade.ttypes import *
from multiservice.types.ttypes import *
def createSampleRequest(text, serviceNames):
ttext=TText(paragraphs=[TParagraph(text=chunk)
for chunk in re.split(r'\n\n+', text)])
chain = [RequestPart(serviceName=name) for name in serviceNames]
request = ObjectRequest(ttext, chain)
return request
def getThriftTransportAndClient(host, port):
transport = TSocket.TSocket(host, port)
try:
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Multiservice.Client(protocol)
transport.open()
return (transport, client)
except:
transport.close()
raise
def getResultAsJSON(result):
jsonStr = jsonpickle.encode(result, unpicklable=False)
return json.dumps(json.load(StringIO.StringIO(jsonStr)), sort_keys=True, indent=4)
def go():
parser = OptionParser()
parser.add_option('-p', '--port', type='int', action='store',
dest='port', default=20000,
help='multiservice port; default: 20000')
parser.add_option('--host', type='string', action='store',
dest='host', default='multiservice.nlp.ipipan.waw.pl',
help='multiservice host; default: multiservice.nlp.ipipan.waw.pl')
(opts, args) = parser.parse_args()
if len(args) == 0:
print "Processing chain was not specified!"
return
transport, client = getThriftTransportAndClient(opts.host, opts.port)
request = createSampleRequest(sys.stdin.read(), args)
try:
token = client.putObjectRequest(request)
status = None
while status not in [RequestStatus.DONE, RequestStatus.FAILED]:
status = client.getRequestStatus(token)
time.sleep(0.1)
if status == RequestStatus.DONE:
result = client.getResultObject(token)
print getResultAsJSON(result)
else:
print >> sys.stderr, client.getException(token)
finally:
transport.close()
if __name__ == '__main__':
go()