piker
piker copied to clipboard
Graphics update throttling
trafficstars
This became a requirement after deploying binance feeds which hit trade / book quote rates in the 500-1k Hz instantaneous rate range (with high burstiness obvs) pretty often. The original patch is in #177.
Premises:
- there's no point in updating graphics faster then the local display rate
- the max update rate should be also limited by measured latency in python "draw cycles" (i.e. the min wait period should be the max refresh refresh period plus additionally measured processing time from python code in update loops)
- taking a naive approach (no fancy v-sync type stuff yet) will at most update graphics once per physical display cycle even if the synchronization with hardware is off
- limiting to refresh rate should mitigate most forms of tearing presuming we keep updates below the display rate
- there might be a way to actually hook into
Qtdraw cycle events usingQPaintEngine.updateStatewhich would maybe allow us to check for state that would suggest that a draw cycle is imminent?
TODOs for the interested graphics-onistas
- [ ] allow specifying the throttle rate in a
piker.toml - [ ] consider a less naive approach then our current hard coded rounding to the
QScreen.refreshRate() - [ ] offer the throttle api through a composable generator?
- maybe something like:
async def throttle_to(async_iter: AsyncIterable[T], rate:int = 60) -> T: last = time.time() processing_total = 0.001 # heuristic default async for val in async_iter: if time.time() - last <= 1/rate - processing_total: continue else: processing_start = time.time() yield val processing_total = time.time() - processing_start last = time.time()
- maybe something like:
Some further resources from #177 re: v-sync (though not sure if we should dig this deep yet):
- SO question on syncing images with refresh rate
- answer points to SO on
updateGL()realtime in Qt which requires V-sync
- answer points to SO on
- SO on missing frames with v-sync and a matching Qt forum post
not sure why #192 isn't linked here, but it should be.