[+] improve timeouts handling

This commit is contained in:
Siarhei Siniak 2025-07-10 11:40:53 +03:00
parent 92a9f36acd
commit 52df4b54d5
2 changed files with 40 additions and 12 deletions

@ -27,6 +27,10 @@ async def run() -> None:
timestamp: datetime.datetime,
session: 'async_sessionmaker[AsyncSession]',
) -> None:
logger.info(dict(
msg='before markets',
))
markets = await markets_get_by_symbol(
session,
set([
@ -35,6 +39,10 @@ async def run() -> None:
]),
)
logger.info(dict(
msg='after markets',
))
await ticker_store_multiple(
session,
[
@ -55,7 +63,9 @@ async def run() -> None:
await Emcont.worker(
only_symbols={'EURUSD', 'USDJPY', 'GBPUSD', 'AUDUSD', 'USDCAD'},
session=async_session,
store_cb=store_cb
store_cb=store_cb,
request_timeout=2,
store_timeout=0.5,
)
if __name__ == '__main__':

@ -92,27 +92,45 @@ class Emcont:
session: 'async_sessionmaker[AsyncSession]',
store_cb: 'Emcont.store_cb_t',
only_symbols: Optional[set[str]] = None,
request_timeout: float | int = 0.5,
store_timeout: float | int = 0.5,
) -> None:
last_retrieval = datetime.datetime.now()
assert request_timeout >= 0
assert store_timeout >= 0
while True:
logger.info(dict(msg='started'))
entries : Optional[cls.rates_get_t.data_t] = None
try:
try:
async with asyncio.timeout(request_timeout):
entries = await cls.rates_get(
only_symbols=only_symbols,
)
except TimeoutError:
logger.exception('request timeout')
try:
async with asyncio.timeout(store_timeout):
if entries:
await store_cb(
rates=entries.rates,
timestamp=last_retrieval,
session=session,
)
except TimeoutError:
logger.exception('store timeout')
except:
logger.exception('')
next_retrieval = last_retrieval + datetime.timedelta(seconds=1)
wait_interval = (
datetime.datetime.now() - next_retrieval
next_retrieval - datetime.datetime.now()
).total_seconds()
if wait_interval > 0: