Rework ohlc sampling to launch from .start()

Avoid bothering with a trio event and expect the caller to do manual shm
registering with the write loop. Provide OHLC sample period indexing
through a re-branded pub-sub func ``iter_ohlc_periods()``.
cached_feeds
Tyler Goodlet 2021-04-03 01:18:51 -04:00
parent a8a3f098cf
commit c05fc8991a
1 changed files with 79 additions and 43 deletions

View File

@ -17,17 +17,25 @@
""" """
Data buffers for fast shared humpy. Data buffers for fast shared humpy.
""" """
from typing import Tuple, Callable, Dict from typing import Dict, List
# import time
import tractor import tractor
import trio import trio
from trio_typing import TaskStatus
from ._sharedmem import ShmArray from ._sharedmem import ShmArray
from ..log import get_logger
_shms: Dict[int, ShmArray] = {} log = get_logger(__name__)
# TODO: we could stick these in a composed type to avoid
# angering the "i hate module scoped variables crowd" (yawn).
_shms: Dict[int, List[ShmArray]] = {}
_start_increment: Dict[str, trio.Event] = {} _start_increment: Dict[str, trio.Event] = {}
_incrementers: Dict[int, trio.CancelScope] = {}
_subscribers: Dict[str, tractor.Context] = {}
def shm_incrementing(shm_token_name: str) -> trio.Event: def shm_incrementing(shm_token_name: str) -> trio.Event:
@ -35,11 +43,9 @@ def shm_incrementing(shm_token_name: str) -> trio.Event:
return _start_increment.setdefault(shm_token_name, trio.Event()) return _start_increment.setdefault(shm_token_name, trio.Event())
@tractor.msg.pub
async def increment_ohlc_buffer( async def increment_ohlc_buffer(
shm_token: dict, delay_s: int,
get_topics: Callable[..., Tuple[str]], task_status: TaskStatus[trio.CancelScope] = trio.TASK_STATUS_IGNORED,
# delay_s: Optional[float] = None,
): ):
"""Task which inserts new bars into the provide shared memory array """Task which inserts new bars into the provide shared memory array
every ``delay_s`` seconds. every ``delay_s`` seconds.
@ -54,14 +60,16 @@ async def increment_ohlc_buffer(
the underlying buffers will actually be incremented. the underlying buffers will actually be incremented.
""" """
# wait for brokerd to signal we should start sampling # # wait for brokerd to signal we should start sampling
await shm_incrementing(shm_token['shm_name']).wait() # await shm_incrementing(shm_token['shm_name']).wait()
# TODO: right now we'll spin printing bars if the last time stamp is # TODO: right now we'll spin printing bars if the last time stamp is
# before a large period of no market activity. Likely the best way # before a large period of no market activity. Likely the best way
# to solve this is to make this task aware of the instrument's # to solve this is to make this task aware of the instrument's
# tradable hours? # tradable hours?
global _incrementers
# adjust delay to compensate for trio processing time # adjust delay to compensate for trio processing time
ad = min(_shms.keys()) - 0.001 ad = min(_shms.keys()) - 0.001
@ -69,6 +77,12 @@ async def increment_ohlc_buffer(
lowest = min(_shms.keys()) lowest = min(_shms.keys())
ad = lowest - 0.001 ad = lowest - 0.001
with trio.CancelScope() as cs:
# register this time period step as active
_incrementers[delay_s] = cs
task_status.started(cs)
while True: while True:
# TODO: do we want to support dynamically # TODO: do we want to support dynamically
# adding a "lower" lowest increment period? # adding a "lower" lowest increment period?
@ -103,13 +117,35 @@ async def increment_ohlc_buffer(
shm.push(last) shm.push(last)
# broadcast the buffer index step # broadcast the buffer index step
yield {'index': shm._last.value} # yield {'index': shm._last.value}
for ctx in _subscribers.get(delay_s, ()):
try:
await ctx.send_yield({'index': shm._last.value})
except (
trio.BrokenResourceError,
trio.ClosedResourceError
):
log.error(f'{ctx.chan.uid} dropped connection')
def subscribe_ohlc_for_increment( @tractor.stream
shm: ShmArray, async def iter_ohlc_periods(
delay: int, ctx: tractor.Context,
delay_s: int,
) -> None: ) -> None:
"""Add an OHLC ``ShmArray`` to the increment set.
""" """
_shms.setdefault(delay, []).append(shm) Subscribe to OHLC sampling "step" events: when the time
aggregation period increments, this event stream emits an index
event.
"""
# add our subscription
global _subscribers
subs = _subscribers.setdefault(delay_s, [])
subs.append(ctx)
try:
# stream and block until cancelled
await trio.sleep_forever()
finally:
subs.remove(ctx)