/
_clientsocket.py
106 lines (85 loc) · 2.48 KB
/
_clientsocket.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import netutils
import config
from tornado import tcpclient
from tornado import gen
from tornado.ioloop import IOLoop
import time
import os
import resource
resource.setrlimit(resource.RLIMIT_NOFILE, (10000, 10000))
class Client():
def __init__(self, host, port):
self.tcpClient = tcpclient.TCPClient()
self.mode = config.SYNC_MODE
self.host = host
self.port = port
# @gen.coroutine
# def _connect(self):
# self.stream = yield self.tcpClient.connect(self.host, self.port)
# def connect(self, host, port):
# self.host = host
# self.port = port
# IOLoop.current().run_sync(self._connect)
@gen.coroutine
def _connect(self, cb):
stream = yield self.tcpClient.connect(self.host, self.port)
cb(stream)
def call(self, method_name, params, cb):
def call_async(stream):
# print 'connect cb'
msg = {
'msgid': next(generator),
'method': method_name,
'params': params,
'mode': self.mode
}
# print 'send msgid:', msg['msgid'], os.getpid()
def recv_cb(data):
# print 'send cb'
stream.close()
cb(data)
netutils.send(stream, msg, callback=lambda: netutils.recv(stream, recv_cb))
self._connect(call_async)
def set_sync(self):
self.mode = config.SYNC_MODE
def set_async(self):
self.mode = config.ASYNC_MODE
def msgid_generator():
counter = 0
while True:
yield counter
counter += 1
if counter > (1 << 30):
counter = 0
generator = msgid_generator()
# -----------------------
start = time.time()
def cb1(data):
# print 'cb1:', data
end = time.time()
print end - start
pass
# print 'cb1:', data
def test():
# start = time.time()
for i in xrange(3000):
# ts = time.time()
# print 'before client'
client = Client('127.0.0.1', 8000)
# client.set_async()
# client.connect('127.0.0.1', 8000)
# print 'before call'
client.call('sum', [i, i+1], cb1)
# data = client.call('sum', [i, i+1])
# print data
# te = time.time()
# print te-ts
end = time.time()
print end - start
if __name__ == '__main__':
def cb2(data):
print 'cb2:', data
# client.connect('127.0.0.1', 8000)
test()
IOLoop.current().start()
# client.start()