Merge pull request #177 from pikers/update_throttling

Naively throttle graphics updates to 60 FPS
binance_syminfo_and_mintick
goodboy 2021-05-25 08:33:19 -04:00 committed by GitHub
commit b03fd80a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 3 deletions

View File

@ -18,6 +18,7 @@
High level Qt chart widgets. High level Qt chart widgets.
""" """
import time
from typing import Tuple, Dict, Any, Optional, Callable from typing import Tuple, Dict, Any, Optional, Callable
from types import ModuleType from types import ModuleType
from functools import partial from functools import partial
@ -934,7 +935,9 @@ async def test_bed(
# txt.setPos(vb_right - w, d_coords.y()) # txt.setPos(vb_right - w, d_coords.y())
# txt.setPlainText(f"FUCK YOU {i}") # txt.setPlainText(f"FUCK YOU {i}")
i += 1 i += 1
# rlabel.setPos(vb_right - 2*w, d_coords.y())
_quote_throttle_rate: int = 60 # Hz
async def chart_from_quotes( async def chart_from_quotes(
@ -1011,9 +1014,17 @@ async def chart_from_quotes(
tick_size = chart._lc.symbol.tick_size tick_size = chart._lc.symbol.tick_size
tick_margin = 2 * tick_size tick_margin = 2 * tick_size
last = time.time()
async for quotes in stream: async for quotes in stream:
for sym, quote in quotes.items():
now = time.time()
period = now - last
if period <= 1/_quote_throttle_rate - 0.001:
# faster then display refresh rate
# print(f'quote too fast: {1/period}')
continue
for sym, quote in quotes.items():
for tick in quote.get('ticks', ()): for tick in quote.get('ticks', ()):
# print(f"CHART: {quote['symbol']}: {tick}") # print(f"CHART: {quote['symbol']}: {tick}")
@ -1099,6 +1110,9 @@ async def chart_from_quotes(
last_mx, last_mn = mx, mn last_mx, last_mn = mx, mn
# set time of last graphics update
last = now
async def spawn_fsps( async def spawn_fsps(
linked_charts: LinkedSplitCharts, linked_charts: LinkedSplitCharts,
@ -1272,9 +1286,19 @@ async def run_fsp(
stream = conf['stream'] stream = conf['stream']
last = time.time()
# update chart graphics # update chart graphics
async for value in stream: async for value in stream:
now = time.time()
period = now - last
# if period <= 1/30:
if period <= 1/_quote_throttle_rate - 0.001:
# faster then display refresh rate
# print(f'quote too fast: {1/period}')
continue
# TODO: provide a read sync mechanism to avoid this polling. # TODO: provide a read sync mechanism to avoid this polling.
# the underlying issue is that a backfill and subsequent shm # the underlying issue is that a backfill and subsequent shm
# array first/last index update could result in an empty array # array first/last index update could result in an empty array
@ -1299,6 +1323,9 @@ async def run_fsp(
# update graphics # update graphics
chart.update_curve_from_array(fsp_func_name, array) chart.update_curve_from_array(fsp_func_name, array)
# set time of last graphics update
last = now
async def check_for_new_bars(feed, ohlcv, linked_charts): async def check_for_new_bars(feed, ohlcv, linked_charts):
"""Task which updates from new bars in the shared ohlcv buffer every """Task which updates from new bars in the shared ohlcv buffer every
@ -1506,10 +1533,22 @@ async def _async_main(
Provision the "main" widget with initial symbol data and root nursery. Provision the "main" widget with initial symbol data and root nursery.
""" """
chart_app = widgets['main'] chart_app = widgets['main']
# attempt to configure DPI aware font size # attempt to configure DPI aware font size
_font.configure_to_dpi(current_screen()) screen = current_screen()
# configure graphics update throttling based on display refresh rate
global _quote_throttle_rate
_quote_throttle_rate = min(
round(screen.refreshRate()),
_quote_throttle_rate,
)
log.info(f'Set graphics update rate to {_quote_throttle_rate} Hz')
# configure global DPI aware font size
_font.configure_to_dpi(screen)
async with trio.open_nursery() as root_n: async with trio.open_nursery() as root_n: