Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 5b4610f

Browse files
committed
- Stream error handling
- Pass PING message to the stream.
1 parent 7f9cd5c commit 5b4610f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

thetadata/client.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def connect_stream(self, callback) -> Thread:
335335
raise ConnectionError('Unable to connect to the local Theta Terminal Stream process. '
336336
'Try restarting your system.')
337337
sleep(1)
338-
self._stream_server.settimeout(self.timeout)
338+
self._stream_server.settimeout(10)
339339
self._stream_impl = callback
340340
out = Thread(target=self._recv_stream)
341341
out.start()
@@ -487,7 +487,7 @@ def _recv_stream(self):
487487
msg = StreamMsg()
488488
msg.client = self
489489
parse_int = lambda d: int.from_bytes(d, "big")
490-
490+
self._stream_server.settimeout(10)
491491
while True:
492492
try:
493493
msg.type = StreamMsgType.from_code(parse_int(self._read_stream(1)[:1]))
@@ -502,7 +502,6 @@ def _recv_stream(self):
502502
msg.ohlcvc.from_bytes(data)
503503
elif msg.type == StreamMsgType.PING:
504504
self._read_stream(n_bytes=4)
505-
continue
506505
elif msg.type == StreamMsgType.OPEN_INTEREST:
507506
data = self._read_stream(n_bytes=8)
508507
msg.open_interest.from_bytes(data)
@@ -516,16 +515,19 @@ def _recv_stream(self):
516515
self._read_stream(4) # Future use.
517516
else:
518517
raise ValueError('undefined msg type: ' + str(msg.type))
519-
520-
self._stream_impl(msg)
521518
except ConnectionResetError:
522519
msg.type = StreamMsgType.STREAM_DEAD
523520
self._stream_impl(msg)
524521
return
522+
except Exception as e:
523+
msg.type = StreamMsgType.ERROR
524+
print('Stream error: ' + str(e))
525+
self._stream_impl(msg)
525526

526527
def _read_stream(self, n_bytes: int) -> bytearray:
527528
"""from_bytes
528529
"""
530+
529531
buffer = bytearray(self._stream_server.recv(n_bytes))
530532
total = buffer.__len__()
531533

0 commit comments

Comments
 (0)