Skip to content

Commit cf9222a

Browse files
author
Sam McHardy
authored
Merge pull request sammchardy#280 from JSRossiter/dcm-shared-socket
allow DepthCacheManager to share a SocketManager
2 parents ba43645 + 6876ec8 commit cf9222a

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

binance/depthcache.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class DepthCacheManager(object):
120120

121121
_default_refresh = 60 * 30 # 30 minutes
122122

123-
def __init__(self, client, symbol, callback=None, refresh_interval=_default_refresh):
123+
def __init__(self, client, symbol, callback=None, refresh_interval=_default_refresh, bm=None):
124124
"""Initialise the DepthCacheManager
125125
126126
:param client: Binance API client
@@ -138,9 +138,10 @@ def __init__(self, client, symbol, callback=None, refresh_interval=_default_refr
138138
self._callback = callback
139139
self._last_update_id = None
140140
self._depth_message_buffer = []
141-
self._bm = None
141+
self._bm = bm
142142
self._depth_cache = DepthCache(self._symbol)
143143
self._refresh_interval = refresh_interval
144+
self._conn_key = None
144145

145146
self._start_socket()
146147
self._init_cache()
@@ -180,11 +181,13 @@ def _start_socket(self):
180181
181182
:return:
182183
"""
183-
self._bm = BinanceSocketManager(self._client)
184+
if self._bm is None:
185+
self._bm = BinanceSocketManager(self._client)
184186

185-
self._bm.start_depth_socket(self._symbol, self._depth_event)
186-
187-
self._bm.start()
187+
self._conn_key = self._bm.start_depth_socket(
188+
self._symbol, self._depth_event)
189+
if not self._bm.is_alive():
190+
self._bm.start()
188191

189192
# wait for some socket responses
190193
while not len(self._depth_message_buffer):
@@ -252,10 +255,13 @@ def get_depth_cache(self):
252255
"""
253256
return self._depth_cache
254257

255-
def close(self):
258+
def close(self, close_socket=False):
256259
"""Close the open socket for this manager
257260
258261
:return:
259262
"""
260-
self._bm.close()
263+
self._bm.stop_socket(self._conn_key)
264+
if close_socket:
265+
self._bm.close()
266+
time.sleep(1)
261267
self._depth_cache = None

0 commit comments

Comments
 (0)