Add a `DataFeed.call_client()` method

Allows for calling an actor local broker client's methods from a remote
actor.
kivy_mainline_and_py3.8
Tyler Goodlet 2019-02-09 21:38:00 -05:00
parent 026b015627
commit f6230dd6df
1 changed files with 45 additions and 24 deletions

View File

@ -25,7 +25,7 @@ log = get_logger('broker.data')
async def wait_for_network(net_func: Callable, sleep: int = 1) -> dict: async def wait_for_network(net_func: Callable, sleep: int = 1) -> dict:
"""Wait until the network comes back up. """Wait until the network (DNS) comes back up.
""" """
down = False down = False
while True: while True:
@ -220,7 +220,7 @@ async def get_cached_feed(
ss = tractor.current_actor().statespace ss = tractor.current_actor().statespace
feeds = ss.setdefault('feeds', {'_lock': trio.Lock()}) feeds = ss.setdefault('feeds', {'_lock': trio.Lock()})
lock = feeds['_lock'] lock = feeds['_lock']
try: feed = None
try: try:
async with lock: async with lock:
feed = feeds[brokername] feed = feeds[brokername]
@ -241,6 +241,7 @@ async def get_cached_feed(
feeds[brokername] = feed feeds[brokername] = feed
yield feed yield feed
finally: finally:
if feed is not None:
# destroy the API client # destroy the API client
await feed.exit_stack.aclose() await feed.exit_stack.aclose()
@ -319,9 +320,18 @@ async def start_quote_stream(
f"Terminating stream quoter task for {feed.mod.name}") f"Terminating stream quoter task for {feed.mod.name}")
async def call_client(
broker: str,
methname: str,
**kwargs,
):
async with get_cached_feed(broker) as feed:
return await getattr(feed.client, methname)(**kwargs)
class DataFeed: class DataFeed:
"""Data feed client for streaming symbol data from a (remote) """Data feed client for streaming symbol data from and making API client calls
``brokerd`` data daemon. to a (remote) ``brokerd`` daemon.
""" """
_allowed = ('stock', 'option') _allowed = ('stock', 'option')
@ -400,6 +410,17 @@ class DataFeed:
]) ])
return records, displayables return records, displayables
async def call_client(self, method, **kwargs):
"""Call a broker ``Client`` method using RPC and return result.
"""
return await self.portal.run(
'piker.brokers.data',
'call_client',
broker=self.brokermod.name,
methname=method,
**kwargs
)
async def stream_to_file( async def stream_to_file(
watchlist_name: str, watchlist_name: str,