Return early on remote actor lookups instead of branching
parent
8ce37875a0
commit
4ab8545e87
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
pikerd daemon lifecylcle & rpc
|
pikerd daemon lifecylcle & rpc
|
||||||
"""
|
"""
|
||||||
from typing import Optional, Dict, Callable
|
from typing import Optional
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import tractor
|
import tractor
|
||||||
|
@ -23,35 +23,32 @@ _root_modules = [
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def open_pikerd(
|
async def open_pikerd(
|
||||||
loglevel: Optional[str] = None
|
loglevel: Optional[str] = None,
|
||||||
|
**kwargs,
|
||||||
) -> Optional[tractor._portal.Portal]:
|
) -> Optional[tractor._portal.Portal]:
|
||||||
|
|
||||||
global _root_nursery
|
global _root_nursery
|
||||||
|
|
||||||
async with tractor.open_root_actor(
|
# XXX: this may open a root actor as well
|
||||||
|
async with tractor.open_nursery(
|
||||||
name=_root_dname,
|
name=_root_dname,
|
||||||
loglevel=loglevel,
|
|
||||||
|
|
||||||
# TODO: eventually we should be able to avoid
|
# TODO: eventually we should be able to avoid
|
||||||
# having the root have more then permissions to
|
# having the root have more then permissions to
|
||||||
# spawn other specialized daemons I think?
|
# spawn other specialized daemons I think?
|
||||||
# enable_modules=[__name__],
|
# enable_modules=[__name__],
|
||||||
enable_modules=_root_modules
|
enable_modules=_root_modules,
|
||||||
):
|
|
||||||
# init root nursery
|
loglevel=loglevel,
|
||||||
try:
|
) as nursery:
|
||||||
async with tractor.open_nursery() as nursery:
|
|
||||||
_root_nursery = nursery
|
_root_nursery = nursery
|
||||||
yield nursery
|
yield nursery
|
||||||
finally:
|
|
||||||
# client code may block indefinitely so cancel when
|
|
||||||
# teardown is invoked
|
|
||||||
await nursery.cancel()
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def maybe_open_pikerd(
|
async def maybe_open_pikerd(
|
||||||
loglevel: Optional[str] = None
|
loglevel: Optional[str] = None,
|
||||||
|
**kwargs,
|
||||||
) -> Optional[tractor._portal.Portal]:
|
) -> Optional[tractor._portal.Portal]:
|
||||||
"""If no ``pikerd`` daemon-root-actor can be found,
|
"""If no ``pikerd`` daemon-root-actor can be found,
|
||||||
assume that role and return a portal to myself
|
assume that role and return a portal to myself
|
||||||
|
@ -62,13 +59,22 @@ async def maybe_open_pikerd(
|
||||||
if loglevel:
|
if loglevel:
|
||||||
get_console_log(loglevel)
|
get_console_log(loglevel)
|
||||||
|
|
||||||
|
try:
|
||||||
async with tractor.find_actor(_root_dname) as portal:
|
async with tractor.find_actor(_root_dname) as portal:
|
||||||
|
|
||||||
if portal is not None: # pikerd exists
|
if portal is not None: # pikerd exists
|
||||||
yield portal
|
yield portal
|
||||||
|
return
|
||||||
|
|
||||||
|
except RuntimeError: # tractor runtime not started yet
|
||||||
|
pass
|
||||||
|
|
||||||
|
# assume pikerd role
|
||||||
|
async with open_pikerd(
|
||||||
|
loglevel,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
assert _root_nursery
|
||||||
|
|
||||||
else: # assume role
|
|
||||||
async with open_pikerd(loglevel) as nursery:
|
|
||||||
# in the case where we're starting up the
|
# in the case where we're starting up the
|
||||||
# tractor-piker runtime stack in **this** process
|
# tractor-piker runtime stack in **this** process
|
||||||
# we want to hand off a nursery for starting (as a sub)
|
# we want to hand off a nursery for starting (as a sub)
|
||||||
|
@ -101,12 +107,14 @@ async def spawn_brokerd(
|
||||||
|
|
||||||
# TODO: raise exception when _root_nursery == None?
|
# TODO: raise exception when _root_nursery == None?
|
||||||
global _root_nursery
|
global _root_nursery
|
||||||
portal = await _root_nursery.start_actor(
|
|
||||||
|
await _root_nursery.start_actor(
|
||||||
dname,
|
dname,
|
||||||
enable_modules=_data_mods + [brokermod.__name__],
|
enable_modules=_data_mods + [brokermod.__name__],
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
**tractor_kwargs
|
**tractor_kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
return dname
|
return dname
|
||||||
|
|
||||||
|
|
||||||
|
@ -116,15 +124,12 @@ async def spawn_emsd(
|
||||||
**extra_tractor_kwargs
|
**extra_tractor_kwargs
|
||||||
) -> tractor._portal.Portal:
|
) -> tractor._portal.Portal:
|
||||||
|
|
||||||
from .clearing import _client
|
|
||||||
|
|
||||||
log.info('Spawning emsd')
|
log.info('Spawning emsd')
|
||||||
|
|
||||||
# TODO: raise exception when _root_nursery == None?
|
# TODO: raise exception when _root_nursery == None?
|
||||||
global _root_nursery
|
global _root_nursery
|
||||||
assert _root_nursery
|
|
||||||
|
|
||||||
portal = await _root_nursery.start_actor(
|
await _root_nursery.start_actor(
|
||||||
'emsd',
|
'emsd',
|
||||||
enable_modules=[
|
enable_modules=[
|
||||||
'piker.clearing._ems',
|
'piker.clearing._ems',
|
||||||
|
|
Loading…
Reference in New Issue