6
6
import msgpack
7
7
import re
8
8
import websockets
9
- import queue
10
9
11
10
from .common import get_base_url , get_data_stream_url , get_credentials , URL
12
11
from .entity import Entity
@@ -59,7 +58,6 @@ def __init__(self,
59
58
self ._running = False
60
59
self ._loop = None
61
60
self ._raw_data = raw_data
62
- self ._stop_stream_queue = queue .Queue ()
63
61
self ._handlers = {
64
62
'trades' : {},
65
63
'quotes' : {},
@@ -113,26 +111,14 @@ async def close(self):
113
111
114
112
async def stop_ws (self ):
115
113
self ._should_run = False
116
- if self ._stop_stream_queue .empty ():
117
- self ._stop_stream_queue .put_nowait ({"should_stop" : True })
114
+ await self .close ()
118
115
119
116
async def _consume (self ):
120
117
while True :
121
- if not self ._stop_stream_queue .empty ():
122
- self ._stop_stream_queue .get (timeout = 1 )
123
- await self .close ()
124
- break
125
- else :
126
- try :
127
- r = await asyncio .wait_for (self ._ws .recv (), 5 )
128
- msgs = msgpack .unpackb (r )
129
- for msg in msgs :
130
- await self ._dispatch (msg )
131
- except asyncio .TimeoutError :
132
- # ws.recv is hanging when no data is received. by using
133
- # wait_for we break when no data is received, allowing us
134
- # to break the loop when needed
135
- pass
118
+ r = await self ._ws .recv ()
119
+ msgs = msgpack .unpackb (r )
120
+ for msg in msgs :
121
+ await self ._dispatch (msg )
136
122
137
123
def _cast (self , msg_type , msg ):
138
124
result = msg
@@ -230,14 +216,10 @@ async def _run_forever(self):
230
216
v for k , v in self ._handlers .items ()
231
217
if k not in ("cancelErrors" , "corrections" )
232
218
):
233
- if not self ._stop_stream_queue .empty ():
234
- # the ws was signaled to stop before starting the loop so
235
- # we break
236
- self ._stop_stream_queue .get (timeout = 1 )
219
+ if not self ._should_run :
237
220
return
238
221
await asyncio .sleep (0.1 )
239
222
log .info (f'started { self ._name } stream' )
240
- self ._should_run = True
241
223
self ._running = False
242
224
while True :
243
225
try :
@@ -253,10 +235,10 @@ async def _run_forever(self):
253
235
self ._running = True
254
236
await self ._consume ()
255
237
except websockets .WebSocketException as wse :
256
- await self .close ()
257
- self . _running = False
258
- log .warn ('data websocket error, restarting connection: ' +
259
- str (wse ))
238
+ if self ._should_run :
239
+ await self . close ()
240
+ log .warn ('data websocket error, restarting connection: ' +
241
+ str (wse ))
260
242
except Exception as e :
261
243
log .exception ('error during websocket '
262
244
'communication: {}' .format (str (e )))
@@ -610,7 +592,6 @@ def __init__(self,
610
592
self ._running = False
611
593
self ._loop = None
612
594
self ._raw_data = raw_data
613
- self ._stop_stream_queue = queue .Queue ()
614
595
self ._should_run = True
615
596
self ._websocket_params = websocket_params
616
597
@@ -675,31 +656,18 @@ async def _start_ws(self):
675
656
676
657
async def _consume (self ):
677
658
while True :
678
- if not self ._stop_stream_queue .empty ():
679
- self ._stop_stream_queue .get (timeout = 1 )
680
- await self .close ()
681
- break
682
- else :
683
- try :
684
- r = await asyncio .wait_for (self ._ws .recv (), 5 )
685
- msg = json .loads (r )
686
- await self ._dispatch (msg )
687
- except asyncio .TimeoutError :
688
- # ws.recv is hanging when no data is received. by using
689
- # wait_for we break when no data is received, allowing us
690
- # to break the loop when needed
691
- pass
659
+ r = await self ._ws .recv ()
660
+ msg = json .loads (r )
661
+ await self ._dispatch (msg )
692
662
693
663
async def _run_forever (self ):
694
664
self ._loop = asyncio .get_running_loop ()
695
665
# do not start the websocket connection until we subscribe to something
696
666
while not self ._trade_updates_handler :
697
- if not self ._stop_stream_queue .empty ():
698
- self ._stop_stream_queue .get (timeout = 1 )
667
+ if not self ._should_run :
699
668
return
700
669
await asyncio .sleep (0.1 )
701
670
log .info ('started trading stream' )
702
- self ._should_run = True
703
671
self ._running = False
704
672
while True :
705
673
try :
@@ -712,10 +680,10 @@ async def _run_forever(self):
712
680
self ._running = True
713
681
await self ._consume ()
714
682
except websockets .WebSocketException as wse :
715
- await self .close ()
716
- self . _running = False
717
- log .warn ('trading stream websocket error, restarting ' +
718
- ' connection: ' + str (wse ))
683
+ if self ._should_run :
684
+ await self . close ()
685
+ log .warn ('trading stream websocket error, restarting ' +
686
+ ' connection: ' + str (wse ))
719
687
except Exception as e :
720
688
log .exception ('error during websocket '
721
689
'communication: {}' .format (str (e )))
@@ -730,8 +698,7 @@ async def close(self):
730
698
731
699
async def stop_ws (self ):
732
700
self ._should_run = False
733
- if self ._stop_stream_queue .empty ():
734
- self ._stop_stream_queue .put_nowait ({"should_stop" : True })
701
+ await self .close ()
735
702
736
703
def stop (self ):
737
704
if self ._loop .is_running ():
0 commit comments