Remove stream opening lock on `DataFeed`
Fixes to `tractor` that resolve issues with async generators being non-task safe make the need for the mutex lock in `DataFeed.open_stream()` unnecessary. Also, don't bother pushing empty quotes from the publisher; avoids hitting the network when possible.kivy_mainline_and_py3.8
parent
b2322d885c
commit
435b2a56e8
|
@ -1,5 +1,5 @@
|
||||||
"""
|
"""
|
||||||
Live data feed machinery
|
Real-time data feed machinery
|
||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
@ -9,7 +9,11 @@ import socket
|
||||||
import json
|
import json
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
import typing
|
import typing
|
||||||
from typing import Coroutine, Callable, Dict, List, Any, Tuple
|
from typing import (
|
||||||
|
Coroutine, Callable, Dict,
|
||||||
|
List, Any, Tuple, AsyncGenerator,
|
||||||
|
Sequence,
|
||||||
|
)
|
||||||
import contextlib
|
import contextlib
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
|
||||||
|
@ -77,7 +81,7 @@ async def stream_quotes(
|
||||||
get_topics: typing.Callable,
|
get_topics: typing.Callable,
|
||||||
get_quotes: Coroutine,
|
get_quotes: Coroutine,
|
||||||
feed: BrokerFeed,
|
feed: BrokerFeed,
|
||||||
rate: int = 5, # delay between quote requests
|
rate: int = 3, # delay between quote requests
|
||||||
diff_cached: bool = True, # only deliver "new" quotes to the queue
|
diff_cached: bool = True, # only deliver "new" quotes to the queue
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Stream quotes for a sequence of tickers at the given ``rate``
|
"""Stream quotes for a sequence of tickers at the given ``rate``
|
||||||
|
@ -135,10 +139,11 @@ async def stream_quotes(
|
||||||
# quote).
|
# quote).
|
||||||
new_quotes.setdefault(quote['key'], []).append(quote)
|
new_quotes.setdefault(quote['key'], []).append(quote)
|
||||||
else:
|
else:
|
||||||
log.info(f"Delivering quotes:\n{quotes}")
|
# log.debug(f"Delivering quotes:\n{quotes}")
|
||||||
for quote in quotes:
|
for quote in quotes:
|
||||||
new_quotes.setdefault(quote['key'], []).append(quote)
|
new_quotes.setdefault(quote['key'], []).append(quote)
|
||||||
|
|
||||||
|
if new_quotes:
|
||||||
yield new_quotes
|
yield new_quotes
|
||||||
|
|
||||||
# latency monitoring
|
# latency monitoring
|
||||||
|
@ -341,16 +346,20 @@ class DataFeed:
|
||||||
self._quote_type = None
|
self._quote_type = None
|
||||||
self._symbols = None
|
self._symbols = None
|
||||||
self.quote_gen = None
|
self.quote_gen = None
|
||||||
self._mutex = trio.StrictFIFOLock()
|
|
||||||
self._symbol_data_cache: Dict[str, Any] = {}
|
self._symbol_data_cache: Dict[str, Any] = {}
|
||||||
|
|
||||||
async def open_stream(self, symbols, feed_type, rate=1, test=None):
|
async def open_stream(
|
||||||
|
self,
|
||||||
|
symbols: Sequence[str],
|
||||||
|
feed_type: str,
|
||||||
|
rate: int = 1,
|
||||||
|
diff_cached: bool = True,
|
||||||
|
test: bool = None,
|
||||||
|
) -> (AsyncGenerator, dict):
|
||||||
if feed_type not in self._allowed:
|
if feed_type not in self._allowed:
|
||||||
raise ValueError(f"Only feed types {self._allowed} are supported")
|
raise ValueError(f"Only feed types {self._allowed} are supported")
|
||||||
|
|
||||||
self._quote_type = feed_type
|
self._quote_type = feed_type
|
||||||
|
|
||||||
async with self._mutex:
|
|
||||||
try:
|
try:
|
||||||
if self.quote_gen is not None and symbols != self._symbols:
|
if self.quote_gen is not None and symbols != self._symbols:
|
||||||
log.info(
|
log.info(
|
||||||
|
@ -384,6 +393,7 @@ class DataFeed:
|
||||||
broker=self.brokermod.name,
|
broker=self.brokermod.name,
|
||||||
symbols=symbols,
|
symbols=symbols,
|
||||||
feed_type=feed_type,
|
feed_type=feed_type,
|
||||||
|
diff_cached=diff_cached,
|
||||||
rate=rate,
|
rate=rate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue