Cache contracts lookup once at startup

kivy_mainline_and_py3.8
Tyler Goodlet 2018-12-13 13:11:07 -05:00
parent 9e4786e62f
commit 948ee3cadf
1 changed files with 8 additions and 3 deletions

View File

@ -499,15 +499,20 @@ async def option_quoter(client: Client, tickers: List[str]):
raise ValueError(f'Option subscription format is (symbol, expiry)')
@async_lifo_cache(maxsize=128)
async def get_contract_by_date(sym_date_pairs: Tuple[Tuple[str, str]]):
async def get_contract_by_date(
sym_date_pairs: Tuple[Tuple[str, str]],
_contract_cache: dict = {}
):
"""For each tuple,
``(symbol_date_1, symbol_date_2, ... , symbol_date_n)``
return a contract dict.
"""
symbols, dates = zip(*sym_date_pairs)
contracts = await client.get_all_contracts(symbols)
if not _contract_cache:
contracts = await client.get_all_contracts(symbols)
_contract_cache.update(contracts)
selected = {}
for key, val in contracts.items():
for key, val in _contract_cache.items():
if key.expiry in dates:
selected[key] = val