Make shared array buffer incrementer a message pub

Drop ctx manager api and use `tractor.msg.pub`.
bar_select
Tyler Goodlet 2020-09-22 14:30:50 -04:00
parent 373ff90229
commit efb52f2292
1 changed files with 42 additions and 39 deletions

View File

@ -1,18 +1,19 @@
"""
Data buffers for fast shared humpy.
"""
from typing import Tuple, Callable
import time
import tractor
import trio
from ._sharedmem import attach_shared_array
from ._sharedmem import attach_shm_array
@tractor.stream
@tractor.msg.pub
async def incr_buffer(
ctx: tractor.Context,
shm_token: str,
shm_token: dict,
get_topics: Callable[..., Tuple[str]],
# delay_s: Optional[float] = None,
):
"""Task which inserts new bars into the provide shared memory array
@ -23,10 +24,10 @@ async def incr_buffer(
# Likely the best way to solve this is to make this task
# aware of the instrument's tradable hours?
with attach_shared_array(
shm = attach_shm_array(
token=shm_token,
readonly=False,
) as shm:
)
# determine ohlc delay between bars
# to determine time step between datums
@ -39,10 +40,10 @@ async def incr_buffer(
async def sleep():
"""Sleep until next time frames worth has passed from last bar.
"""
last_ts = shm.array[-1]['time']
delay = max((last_ts + ad) - time.time(), 0)
await trio.sleep(delay)
# await trio.sleep(ad)
# last_ts = shm.array[-1]['time']
# delay = max((last_ts + ad) - time.time(), 0)
# await trio.sleep(delay)
await trio.sleep(ad)
while True:
# sleep for duration of current bar
@ -67,5 +68,7 @@ async def incr_buffer(
shm.push(last)
# print('incrementing array')
# yield the new buffer index value
await ctx.send_yield(shm._i.value)
# print(get_topics())
# broadcast the buffer index step
yield {'index': shm._i.value}