forked from goodboy/tractor
1
0
Fork 0

Get rid of dumb random uid and use current actor's uid

246_facepalm_backup
overclockworked64 2021-10-22 02:59:15 +02:00 committed by Tyler Goodlet
parent b7a4641674
commit 04895b9d5e
1 changed files with 10 additions and 14 deletions

View File

@ -14,25 +14,20 @@ import tractor
@acm @acm
async def open_actor_cluster( async def open_actor_cluster(
modules: list[str], modules: list[str],
count: int = cpu_count(), count: int = cpu_count(),
names: Optional[list[str]] = None, names: Optional[list[str]] = None,
start_method: Optional[str] = None, start_method: Optional[str] = None,
hard_kill: bool = False, hard_kill: bool = False,
) -> AsyncGenerator[ ) -> AsyncGenerator[
list[str], list[str],
dict[str, tractor.Portal] dict[str, tractor.Portal]
]: ]:
portals: dict[str, tractor.Portal] = {} portals: dict[str, tractor.Portal] = {}
uid = str(__import__('random').randint(0, 2 ** 16))
# uid = tractor.current_actor().uid
if not names: if not names:
suffix = '_'.join(uid) names = [f'worker_{i}' for i in range(count)]
names = [f'worker_{i}.' + suffix for i in range(count)]
if not len(names) == count: if not len(names) == count:
raise ValueError( raise ValueError(
@ -40,16 +35,17 @@ async def open_actor_cluster(
async with tractor.open_nursery(start_method=start_method) as an: async with tractor.open_nursery(start_method=start_method) as an:
async with trio.open_nursery() as n: async with trio.open_nursery() as n:
for index, key in zip(range(count), names): uid = tractor.current_actor().uid
async def start(i) -> None: async def _start(name: str) -> None:
key = f'worker_{i}.' + '_'.join(uid) name = f'{name}.{uid}'
portals[key] = await an.start_actor( portals[name] = await an.start_actor(
enable_modules=modules, enable_modules=modules,
name=key, name=name,
) )
n.start_soon(start, index) for name in names:
n.start_soon(_start, name)
assert len(portals) == count assert len(portals) == count
yield portals yield portals