From d5d68f75ea4b225e373cd223bb97f1221ff048d3 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 18 Dec 2023 11:45:19 -0500 Subject: [PATCH] ib: only raise first quote timeout err after tries Previously we were actually failing silently too fast instead of actually trying multiple times (now we do for 100) before finally raising any timeout in the final loop `else:` block. --- piker/brokers/ib/api.py | 34 +++++++++++++++++++++------------- piker/brokers/ib/feed.py | 5 +++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/piker/brokers/ib/api.py b/piker/brokers/ib/api.py index 67e63f70..431c889f 100644 --- a/piker/brokers/ib/api.py +++ b/piker/brokers/ib/api.py @@ -843,6 +843,7 @@ class Client: self, contract: Contract, timeout: float = 1, + tries: int = 100, raise_on_timeout: bool = False, ) -> Ticker | None: @@ -857,30 +858,30 @@ class Client: ready: ticker.TickerUpdateEvent = ticker.updateEvent # ensure a last price gets filled in before we deliver quote + timeouterr: Exception | None = None warnset: bool = False - for _ in range(100): - if isnan(ticker.last): + for _ in range(tries): - # wait for a first update(Event) + # wait for a first update(Event) indicatingn a + # live quote feed. + if isnan(ticker.last): try: tkr = await asyncio.wait_for( ready, timeout=timeout, ) - except TimeoutError: - import pdbp - pdbp.set_trace() + if tkr: + break + except TimeoutError as err: + timeouterr = err + await asyncio.sleep(0.01) + continue - if raise_on_timeout: - raise - return None - - if tkr: - break else: if not warnset: log.warning( - f'Quote for {contract} timed out: market is closed?' + f'Quote req timed out..maybe venue is closed?\n' + f'{asdict(contract)}' ) warnset = True @@ -888,6 +889,11 @@ class Client: log.info(f'Got first quote for {contract}') break else: + if timeouterr and raise_on_timeout: + import pdbp + pdbp.set_trace() + raise timeouterr + if not warnset: log.warning( f'Contract {contract} is not returning a quote ' @@ -895,6 +901,8 @@ class Client: ) warnset = True + return None + return ticker # async to be consistent for the client proxy, and cuz why not. diff --git a/piker/brokers/ib/feed.py b/piker/brokers/ib/feed.py index a175209d..c6ed6350 100644 --- a/piker/brokers/ib/feed.py +++ b/piker/brokers/ib/feed.py @@ -943,6 +943,11 @@ async def stream_quotes( contract=con, raise_on_timeout=True, ) + first_quote: dict = normalize(first_ticker) + log.info( + 'Rxed init quote:\n' + f'{pformat(first_quote)}' + ) cs: trio.CancelScope | None = None startup: bool = True while (