-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an async actor cluster spawner prototype
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |