2727from .terminal import check_download , launch_terminal
2828
2929_NOT_CONNECTED_MSG = "You must establish a connection first."
30- _VERSION = '0.9.6 '
30+ _VERSION = '0.9.7 '
3131URL_BASE = "http://127.0.0.1:25510/"
3232
3333
@@ -281,6 +281,7 @@ def __init__(self):
281281 self .contract = Contract ()
282282 self .date = None
283283
284+
284285class ThetaClient :
285286 """A high-level, blocking client used to fetch market data. Instantiating this class
286287 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
317318 self ._stream_responses = {}
318319 self ._counter_lock = threading .Lock ()
319320 self ._stream_req_id = 0
321+ self ._stream_connected = False
320322
321323 print ('If you require API support, feel free to join our discord server! http://discord.thetadata.us' )
322324 if launch :
@@ -382,6 +384,7 @@ def connect_stream(self, callback) -> Thread:
382384 sleep (1 )
383385 self ._stream_server .settimeout (10 )
384386 self ._stream_impl = callback
387+ self ._stream_connected = True
385388 out = Thread (target = self ._recv_stream )
386389 out .start ()
387390 return out
@@ -533,7 +536,7 @@ def _recv_stream(self):
533536 msg .client = self
534537 parse_int = lambda d : int .from_bytes (d , "big" )
535538 self ._stream_server .settimeout (10 )
536- while True :
539+ while self . _stream_connected :
537540 try :
538541 msg .type = StreamMsgType .from_code (parse_int (self ._read_stream (1 )[:1 ]))
539542 msg .contract .from_bytes (self ._read_stream (parse_int (self ._read_stream (1 )[:1 ])))
@@ -560,9 +563,10 @@ def _recv_stream(self):
560563 self ._read_stream (4 ) # Future use.
561564 else :
562565 raise ValueError ('undefined msg type: ' + str (msg .type ))
563- except ConnectionResetError :
566+ except ( ConnectionResetError , OSError ) as e :
564567 msg .type = StreamMsgType .STREAM_DEAD
565568 self ._stream_impl (msg )
569+ self ._stream_connected = False
566570 return
567571 except Exception as e :
568572 msg .type = StreamMsgType .ERROR
0 commit comments