ThriftConnect.py
2.3 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
80
81
82
# coding=utf-8
#author: 4N
#createtime: 2021/9/8
#email: nheweijun@sina.com
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from app.modules.service.image.ImageDataService import ImageDataService
class ThriftConnect:
'''
thrift连接类
'''
client = None
transport = None
def __init__(self,data_server):
host = data_server.split(":")[0]
port = int(data_server.split(":")[1])
socket: TSocket = TSocket.TSocket(host, port)
self.transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = ImageDataService.Client(protocol)
self.transport.open()
def close(self):
self.transport.close()
class ThriftConnectCpp:
'''
thrift连接类
'''
client = None
transport = None
def __init__(self,data_server):
host = data_server.split(":")[0]
port = int(data_server.split(":")[1])
self.transport: TSocket = TSocket.TSocket(host, port)
self.transport = TTransport.TBufferedTransport(self.transport)
protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
# self.client = ImageDataService.Client(protocol)
self.transport.open()
def close(self):
self.transport.close()
class ThriftPool:
'''
thrift线程池
'''
clients = []
transports = []
host = None
port = None
index = -1
def __init__(self,data_server):
self.host = data_server.split(":")[0]
self.port = int(data_server.split(":")[1])
def get_client(self):
if len(self.clients) < 20:
socket: TSocket = TSocket.TSocket(self.host, self.port)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ImageDataService.Client(protocol)
self.clients.append(client)
self.transports.append(transport)
return client,transport
else:
self.index += 1
if self.index == 20:
self.index = 0
return self.clients[self.index],self.transports[self.index]
def close(self):
pass