From a1691cf1c5fe9f5fc7248bcc2a8dd87e295cf80e Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 1 Mar 2021 11:28:44 -0500 Subject: [PATCH] Keep to one EMS daemon if possible --- piker/exchange/_client.py | 44 +++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/piker/exchange/_client.py b/piker/exchange/_client.py index 4bf84828..bebbf039 100644 --- a/piker/exchange/_client.py +++ b/piker/exchange/_client.py @@ -146,6 +146,31 @@ async def send_order_cmds(): yield cmd +@asynccontextmanager +async def maybe_open_emsd( +) -> 'StreamReceiveChannel': # noqa + + async with tractor.find_actor('emsd') as portal: + if portal is not None: + yield portal + + else: + # we gotta spawn it + log.info("Spawning EMS daemon") + + # TODO: add ``maybe_spawn_emsd()`` for this + async with tractor.open_nursery() as n: + + portal = await n.start_actor( + 'emsd', + enable_modules=[ + 'piker.exchange._ems', + ], + ) + + yield portal + + @asynccontextmanager async def open_ems( broker: str, @@ -182,18 +207,16 @@ async def open_ems( - 'dark_cancelled', 'broker_cancelled' - 'dark_executed', 'broker_executed' - 'broker_filled' + """ actor = tractor.current_actor() - # TODO: add ``maybe_spawn_emsd()`` for this - async with tractor.open_nursery() as n: + # wait for service to connect back to us signalling + # ready for order commands + book = get_orders() + + async with maybe_open_emsd() as portal: - portal = await n.start_actor( - 'emsd', - enable_modules=[ - 'piker.exchange._ems', - ], - ) trades_stream = await portal.run( _ems_main, client_actor_name=actor.name, @@ -201,11 +224,6 @@ async def open_ems( symbol=symbol.key, ) - - # wait for service to connect back to us signalling - # ready for order commands - book = get_orders() - with trio.fail_after(10): await book._ready_to_receive.wait()