Skip to content

Commit

Permalink
Add an async actor cluster spawner prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Oct 4, 2021
1 parent cb8144b commit beb2d8f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tractor/_clustering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
Actor cluster helpers.
'''
from contextlib import asynccontextmanager as acm
from multiprocessing import cpu_count
from typing import AsyncGenerator, Optional

import trio
import tractor


@acm
async def open_actor_cluster(

modules: list[str],
count: int = cpu_count(),
names: Optional[list[str]] = None,

) -> AsyncGenerator[..., dict[str, tractor.Portal]]:

portals: dict[str, tractor.Portal] = {}
uid = tractor.current_actor().uid

if not names:
suffix = '_'.join(uid)
names = [f'worker_{i}.' + suffix for i in range(count)]

if not len(names) == count:
raise ValueError(
'Number of names is {len(names)} but count it {count}')

async with (
tractor.open_nursery() as an,
trio.open_nursery() as n,
):
for index, key in zip(range(count), names):

async def start(i) -> None:
key = f'worker_{i}.' + '_'.join(uid)
portals[key] = await an.start_actor(
enable_modules=modules,
name=key,
)

n.start_soon(start, index)

assert len(portals) == count
yield portals

0 comments on commit beb2d8f

Please sign in to comment.