27
27
from .terminal import check_download , launch_terminal
28
28
29
29
_NOT_CONNECTED_MSG = "You must establish a connection first."
30
- _VERSION = '0.9.6 '
30
+ _VERSION = '0.9.7 '
31
31
URL_BASE = "http://127.0.0.1:25510/"
32
32
33
33
@@ -281,6 +281,7 @@ def __init__(self):
281
281
self .contract = Contract ()
282
282
self .date = None
283
283
284
+
284
285
class ThetaClient :
285
286
"""A high-level, blocking client used to fetch market data. Instantiating this class
286
287
runs a java background process, which is responsible for the heavy lifting of market
@@ -317,6 +318,7 @@ def __init__(self, port: int = 11000, timeout: Optional[float] = 60, launch: boo
317
318
self ._stream_responses = {}
318
319
self ._counter_lock = threading .Lock ()
319
320
self ._stream_req_id = 0
321
+ self ._stream_connected = False
320
322
321
323
print ('If you require API support, feel free to join our discord server! http://discord.thetadata.us' )
322
324
if launch :
@@ -382,6 +384,7 @@ def connect_stream(self, callback) -> Thread:
382
384
sleep (1 )
383
385
self ._stream_server .settimeout (10 )
384
386
self ._stream_impl = callback
387
+ self ._stream_connected = True
385
388
out = Thread (target = self ._recv_stream )
386
389
out .start ()
387
390
return out
@@ -533,7 +536,7 @@ def _recv_stream(self):
533
536
msg .client = self
534
537
parse_int = lambda d : int .from_bytes (d , "big" )
535
538
self ._stream_server .settimeout (10 )
536
- while True :
539
+ while self . _stream_connected :
537
540
try :
538
541
msg .type = StreamMsgType .from_code (parse_int (self ._read_stream (1 )[:1 ]))
539
542
msg .contract .from_bytes (self ._read_stream (parse_int (self ._read_stream (1 )[:1 ])))
@@ -560,9 +563,10 @@ def _recv_stream(self):
560
563
self ._read_stream (4 ) # Future use.
561
564
else :
562
565
raise ValueError ('undefined msg type: ' + str (msg .type ))
563
- except ConnectionResetError :
566
+ except ( ConnectionResetError , OSError ) as e :
564
567
msg .type = StreamMsgType .STREAM_DEAD
565
568
self ._stream_impl (msg )
569
+ self ._stream_connected = False
566
570
return
567
571
except Exception as e :
568
572
msg .type = StreamMsgType .ERROR
0 commit comments