Use `Client` in watchlist app

kivy_mainline_and_py3.8
Tyler Goodlet 2018-04-19 00:27:04 -04:00
parent 17feb17535
commit 4123139750
1 changed files with 70 additions and 65 deletions

View File

@ -319,7 +319,7 @@ async def update_quotes(
nursery: 'Nursery',
brokermod: ModuleType,
widgets: dict,
queue: 'StreamQueue',
client: 'Client',
symbol_data: dict,
first_quotes: dict
):
@ -359,7 +359,7 @@ async def update_quotes(
grid.render_rows(cache)
# core cell update loop
async for quotes in queue: # new quotes data only
async for quotes in client.aiter_recv(): # new quotes data only
for symbol, quote in quotes.items():
record, displayable = brokermod.format_quote(
quote, symbol_data=symbol_data)
@ -374,6 +374,7 @@ async def update_quotes(
log.warn("Server connection dropped")
nursery.cancel_scope.cancel()
async def run_kivy(root, nursery):
'''Trio-kivy entry point.
'''
@ -387,18 +388,22 @@ async def _async_main(name, tickers, brokermod, rate):
This is started with cli command `piker watch`.
'''
# setup ticker stream
from ..brokers.core import StreamQueue
queue = StreamQueue(await trio.open_tcp_stream('127.0.0.1', 1616))
await queue.put((brokermod.name, tickers)) # initial request for symbols price streams
from ..brokers.core import Client
async def subscribe(client):
# initial request for symbols price streams
await client.send((brokermod.name, tickers))
async with Client(('127.0.0.1', 1616), subscribe) as client:
# get initial symbol data
async with brokermod.get_client() as client:
async with brokermod.get_client() as bclient:
# get long term data including last days close price
sd = await client.symbol_data(tickers)
sd = await bclient.symbol_data(tickers)
async with trio.open_nursery() as nursery:
# get first quotes response
quotes = await queue.get()
quotes = await client.recv()
first_quotes = [
brokermod.format_quote(quote, symbol_data=sd)[0]
for quote in quotes.values()]
@ -459,4 +464,4 @@ async def _async_main(name, tickers, brokermod, rate):
}
nursery.start_soon(run_kivy, widgets['root'], nursery)
nursery.start_soon(
update_quotes, nursery, brokermod, widgets, queue, sd, quotes)
update_quotes, nursery, brokermod, widgets, client, sd, quotes)