Always do symbol unsubscribe on teardown

kivy_mainline_and_py3.8
Tyler Goodlet 2018-07-05 15:31:52 -04:00
parent 73eedfd7b3
commit f4a91a4975
1 changed files with 62 additions and 59 deletions

View File

@ -391,74 +391,77 @@ async def _async_main(name, portal, tickers, brokermod, rate):
"piker.brokers.core", 'start_quote_stream', "piker.brokers.core", 'start_quote_stream',
broker=brokermod.name, tickers=tickers) broker=brokermod.name, tickers=tickers)
async with trio.open_nursery() as nursery: try:
# get first quotes response async with trio.open_nursery() as nursery:
log.debug("Waiting on first quote...") # get first quotes response
quotes = await agen.__anext__() log.debug("Waiting on first quote...")
first_quotes = [ quotes = await agen.__anext__()
brokermod.format_quote(quote, symbol_data=sd)[0] first_quotes = [
for quote in quotes.values()] brokermod.format_quote(quote, symbol_data=sd)[0]
for quote in quotes.values()]
if first_quotes[0].get('last') is None: if first_quotes[0].get('last') is None:
log.error("Broker API is down temporarily") log.error("Broker API is down temporarily")
nursery.cancel_scope.cancel() nursery.cancel_scope.cancel()
return return
# build out UI # build out UI
Window.set_title(f"watchlist: {name}\t(press ? for help)") Window.set_title(f"watchlist: {name}\t(press ? for help)")
Builder.load_string(_kv) Builder.load_string(_kv)
box = BoxLayout(orientation='vertical', padding=5, spacing=5) box = BoxLayout(orientation='vertical', padding=5, spacing=5)
# define bid-ask "stacked" cells # define bid-ask "stacked" cells
# (TODO: needs some rethinking and renaming for sure) # (TODO: needs some rethinking and renaming for sure)
bidasks = brokermod._bidasks bidasks = brokermod._bidasks
# add header row # add header row
headers = first_quotes[0].keys() headers = first_quotes[0].keys()
header = Row( header = Row(
{key: key for key in headers}, {key: key for key in headers},
headers=headers, headers=headers,
bidasks=bidasks, bidasks=bidasks,
is_header_row=True, is_header_row=True,
size_hint=(1, None), size_hint=(1, None),
) )
box.add_widget(header) box.add_widget(header)
# build grid # build grid
grid = TickerTable( grid = TickerTable(
cols=1, cols=1,
size_hint=(1, None), size_hint=(1, None),
) )
for ticker_record in first_quotes: for ticker_record in first_quotes:
grid.append_row(ticker_record, bidasks=bidasks) grid.append_row(ticker_record, bidasks=bidasks)
# associate the col headers row with the ticker table even though # associate the col headers row with the ticker table even though
# they're technically wrapped separately in containing BoxLayout # they're technically wrapped separately in containing BoxLayout
header.table = grid header.table = grid
# mark the initial sorted column header as bold and underlined # mark the initial sorted column header as bold and underlined
sort_cell = header.get_cell(grid.sort_key) sort_cell = header.get_cell(grid.sort_key)
sort_cell.bold = sort_cell.underline = True sort_cell.bold = sort_cell.underline = True
grid.last_clicked_col_cell = sort_cell grid.last_clicked_col_cell = sort_cell
# set up a pager view for large ticker lists # set up a pager view for large ticker lists
grid.bind(minimum_height=grid.setter('height')) grid.bind(minimum_height=grid.setter('height'))
pager = PagerView(box, grid, nursery) pager = PagerView(box, grid, nursery)
box.add_widget(pager) box.add_widget(pager)
widgets = { widgets = {
# 'anchor': anchor, # 'anchor': anchor,
'root': box, 'root': box,
'grid': grid, 'grid': grid,
'box': box, 'box': box,
'header': header, 'header': header,
'pager': pager, 'pager': pager,
} }
nursery.start_soon( nursery.start_soon(
update_quotes, nursery, brokermod, widgets, agen, sd, quotes) update_quotes, nursery, brokermod, widgets, agen, sd, quotes)
# Trio-kivy entry point. # Trio-kivy entry point.
await async_runTouchApp(widgets['root']) # run kivy await async_runTouchApp(widgets['root']) # run kivy
await agen.aclose() # cancel aysnc gen call await agen.aclose() # cancel aysnc gen call
finally:
# un-subscribe from symbols stream
await portal.run( await portal.run(
"piker.brokers.core", 'modify_quote_stream', "piker.brokers.core", 'modify_quote_stream',
broker=brokermod.name, tickers=[]) broker=brokermod.name, tickers=[])