Skip to content

Commit 6b258e3

Browse files
author
Federico Fissore
committed
Speeding up bridge client. Fixes #4
1 parent 4ac51f3 commit 6b258e3

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

bridge/bridgeclient.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131

3232
class BridgeClient:
33+
def __init__(self):
34+
self.json = None
35+
self.should_close_at_function_end = True
36+
3337
def wait_response(self, json, timeout):
3438
while timeout >= 0:
3539
r = json.recv()
@@ -50,39 +54,57 @@ def wait_key(self, key, json, timeout):
5054
except:
5155
pass
5256

57+
def socket_open(self):
58+
if self.json is None:
59+
self.json = TCPJSONClient('127.0.0.1', 5700)
60+
return self.json
61+
62+
def socket_close(self):
63+
if self.json is not None and self.should_close_at_function_end:
64+
self.json.close()
65+
self.json = None
66+
67+
def begin(self):
68+
self.socket_open()
69+
self.should_close_at_function_end = False
70+
71+
def close(self):
72+
self.should_close_at_function_end = True
73+
self.socket_close()
74+
5375
def get(self, key):
54-
json = TCPJSONClient('127.0.0.1', 5700)
76+
json = self.socket_open()
5577
json.send({'command': 'get', 'key': key})
5678
r = self.wait_key(key, json, 10)
57-
json.close()
79+
self.socket_close()
5880
return r
5981

6082
def getall(self):
61-
json = TCPJSONClient('127.0.0.1', 5700)
83+
json = self.socket_open()
6284
json.send({'command': 'get'})
6385
r = self.wait_response(json, 10)
6486
if not r is None:
6587
r = r['value']
66-
json.close()
88+
self.socket_close()
6789
return r
6890

6991
def put(self, key, value):
70-
json = TCPJSONClient('127.0.0.1', 5700)
92+
json = self.socket_open()
7193
json.send({'command': 'put', 'key': key, 'value': value})
7294
r = self.wait_key(key, json, 10)
73-
json.close()
95+
self.socket_close()
7496
return r
7597

7698
def delete(self, key):
77-
json = TCPJSONClient('127.0.0.1', 5700)
99+
json = self.socket_open()
78100
json.send({'command': 'delete', 'key': key})
79101
r = self.wait_response(json, 10)
80102
if not r is None and not r['value'] is None:
81103
r = r['value']
82-
json.close()
104+
self.socket_close()
83105
return r
84106

85107
def mailbox(self, message):
86-
json = TCPJSONClient('127.0.0.1', 5700)
108+
json = self.socket_open()
87109
json.send({'command': 'raw', 'data': message})
88-
json.close()
110+
self.socket_close()

bridge/bridgeclient_example.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,12 @@
3939

4040
print
4141

42-
print 'Sending mailbox message \'Hello world!\''
43-
client.mailbox('Hello world!')
42+
print 'Using client.begin() and client.close() to speed up consecutive calls to Bridge'
43+
44+
client.begin()
45+
46+
for idx in range(1, 51):
47+
print 'Sending mailbox message \'Hello world ' + str(idx) + '!\''
48+
client.mailbox('Hello world ' + str(idx) + '!')
49+
50+
client.close()

0 commit comments

Comments
 (0)