Port data feed to new tractor stream api

tractor_open_stream_from
Tyler Goodlet 2021-04-29 08:37:35 -04:00
parent 3375735914
commit 0d9f091a34
1 changed files with 62 additions and 57 deletions

View File

@ -51,6 +51,7 @@ from ._sampling import (
iter_ohlc_periods,
sample_and_broadcast,
)
from .ingest import get_ingestormod
log = get_logger(__name__)
@ -302,6 +303,7 @@ class Feed:
async def receive(self) -> dict:
return await self.stream.__anext__()
@asynccontextmanager
async def index_stream(
self,
delay_s: Optional[int] = None
@ -312,14 +314,16 @@ class Feed:
# XXX: this should be singleton on a host,
# a lone broker-daemon per provider should be
# created for all practical purposes
self._index_stream = await self._brokerd_portal.run(
async with self._brokerd_portal.open_stream_from(
iter_ohlc_periods,
delay_s=delay_s or self._max_sample_rate,
)
) as self._index_stream:
yield self._index_stream
else:
yield self._index_stream
return self._index_stream
async def recv_trades_data(self) -> AsyncIterator[dict]:
@asynccontextmanager
async def receive_trades_data(self) -> AsyncIterator[dict]:
if not getattr(self.mod, 'stream_trades', False):
log.warning(
@ -333,7 +337,7 @@ class Feed:
# using the ``_.set_fake_trades_stream()`` method
if self._trade_stream is None:
self._trade_stream = await self._brokerd_portal.run(
async with self._brokerd_portal.open_stream_from(
self.mod.stream_trades,
@ -342,9 +346,10 @@ class Feed:
# in messages, though we could probably use
# more then one?
topics=['local_trades'],
)
return self._trade_stream
) as self._trade_stream:
yield self._trade_stream
else:
yield self._trade_stream
def sym_to_shm_key(
@ -373,17 +378,17 @@ async def open_feed(
# TODO: do all!
sym = symbols[0]
async with maybe_spawn_brokerd(
brokername,
loglevel=loglevel,
) as portal:
# TODO: compress these to one line with py3.9+
async with maybe_spawn_brokerd(brokername, loglevel=loglevel) as portal:
async with portal.open_stream_from(
stream = await portal.run(
attach_feed_bus,
brokername=brokername,
symbol=sym,
loglevel=loglevel,
)
loglevel=loglevel
) as stream:
# TODO: can we make this work better with the proposed
# context based bidirectional streaming style api proposed in: