Move `._cacheables.open_cached_client()` into `.brokers` pkg mod

basic_buy_bot
Tyler Goodlet 2023-06-13 15:22:51 -04:00
parent c0552fa352
commit 921e18728c
8 changed files with 62 additions and 41 deletions

View File

@ -20,9 +20,6 @@ Cacheing apis and toolz.
''' '''
from collections import OrderedDict from collections import OrderedDict
from contextlib import (
asynccontextmanager as acm,
)
from typing import ( from typing import (
Awaitable, Awaitable,
Callable, Callable,
@ -30,12 +27,8 @@ from typing import (
TypeVar, TypeVar,
) )
from tractor.trionics import maybe_open_context
from .brokers import get_brokermod
from .log import get_logger from .log import get_logger
log = get_logger(__name__) log = get_logger(__name__)
T = TypeVar("T") T = TypeVar("T")
@ -104,29 +97,3 @@ def async_lifo_cache(
return decorated return decorated
return decorator return decorator
# TODO: move this to `.brokers.utils`..
@acm
async def open_cached_client(
brokername: str,
**kwargs,
) -> 'Client': # noqa
'''
Get a cached broker client from the current actor's local vars.
If one has not been setup do it and cache it.
'''
brokermod = get_brokermod(brokername)
async with maybe_open_context(
acm_func=brokermod.get_client,
kwargs=kwargs,
) as (cache_hit, client):
if cache_hit:
log.info(f'Reusing existing {client}')
yield client

View File

@ -17,10 +17,34 @@
""" """
Broker clients, daemons and general back end machinery. Broker clients, daemons and general back end machinery.
""" """
from contextlib import (
asynccontextmanager as acm,
)
from importlib import import_module from importlib import import_module
from types import ModuleType from types import ModuleType
__brokers__ = [ from tractor.trionics import maybe_open_context
from ._util import (
log,
BrokerError,
SymbolNotFound,
NoData,
DataUnavailable,
DataThrottle,
resproc,
)
__all__: list[str] = [
'BrokerError',
'SymbolNotFound',
'NoData',
'DataUnavailable',
'DataThrottle',
'resproc',
]
__brokers__: list[str] = [
'binance', 'binance',
'ib', 'ib',
'kraken', 'kraken',
@ -58,3 +82,28 @@ def iter_brokermods():
''' '''
for name in __brokers__: for name in __brokers__:
yield get_brokermod(name) yield get_brokermod(name)
@acm
async def open_cached_client(
brokername: str,
**kwargs,
) -> 'Client': # noqa
'''
Get a cached broker client from the current actor's local vars.
If one has not been setup do it and cache it.
'''
brokermod = get_brokermod(brokername)
async with maybe_open_context(
acm_func=brokermod.get_client,
kwargs=kwargs,
) as (cache_hit, client):
if cache_hit:
log.info(f'Reusing existing {client}')
yield client

View File

@ -29,7 +29,7 @@ import trio
from ._util import log from ._util import log
from . import get_brokermod from . import get_brokermod
from ..service import maybe_spawn_brokerd from ..service import maybe_spawn_brokerd
from .._cacheables import open_cached_client from . import open_cached_client
from ..accounting import MktPair from ..accounting import MktPair

View File

@ -30,7 +30,7 @@ from fuzzywuzzy import process as fuzzy
import numpy as np import numpy as np
import tractor import tractor
from piker._cacheables import open_cached_client from piker.brokers import open_cached_client
from piker.log import get_logger, get_console_log from piker.log import get_logger, get_console_log
from piker.data import ShmArray from piker.data import ShmArray
from piker.brokers._util import ( from piker.brokers._util import (

View File

@ -42,8 +42,10 @@ from piker.accounting._mktinfo import (
Asset, Asset,
MktPair, MktPair,
) )
from piker._cacheables import ( from piker.brokers import (
open_cached_client, open_cached_client,
)
from piker._cacheables import (
async_lifo_cache, async_lifo_cache,
) )
from piker.brokers._util import ( from piker.brokers._util import (

View File

@ -56,8 +56,10 @@ from piker.accounting._mktinfo import (
MktPair, MktPair,
) )
from piker import config from piker import config
from piker._cacheables import ( from piker.brokers import (
open_cached_client, open_cached_client,
)
from piker._cacheables import (
async_lifo_cache, async_lifo_cache,
) )
from piker.log import get_logger from piker.log import get_logger

View File

@ -40,7 +40,8 @@ import wrapt
import asks import asks
from ..calc import humanize, percent_change from ..calc import humanize, percent_change
from .._cacheables import open_cached_client, async_lifo_cache from . import open_cached_client
from piker._cacheables import async_lifo_cache
from .. import config from .. import config
from ._util import resproc, BrokerError, SymbolNotFound from ._util import resproc, BrokerError, SymbolNotFound
from ..log import ( from ..log import (

View File

@ -30,14 +30,14 @@ from typing import (
) )
import msgspec import msgspec
import tractor
import pyqtgraph as pg import pyqtgraph as pg
import tractor
from tractor.trionics import maybe_open_context
import trio import trio
from trio_typing import TaskStatus from trio_typing import TaskStatus
from piker.data.types import Struct from piker.data.types import Struct
from ._axes import PriceAxis from ._axes import PriceAxis
from .._cacheables import maybe_open_context
from ..calc import humanize from ..calc import humanize
from ..data._sharedmem import ( from ..data._sharedmem import (
ShmArray, ShmArray,