-
Notifications
You must be signed in to change notification settings - Fork 24
Add as_actor
single-actor mode
#195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
15a5f0f
add as_actor
DNXie 1aa0dc3
add as_actor
DNXie be146a4
refactor options to support both
DNXie c8a1733
rename num_hosts to hosts
DNXie 72d0315
rename
DNXie aeb6282
refactor actor.py and add more test cases
DNXie be1fbe9
Merge branch 'as_actor' of github.com:DNXie/forge into as_actor
DNXie 782e67d
options stop taking config obj
DNXie 310b04d
fix lint
DNXie 5565b03
Merge remote-tracking branch 'upstream/main' into as_actor
DNXie 9eb7c8f
fix ci
DNXie 56c5b5e
fix broken tests
DNXie 53f773c
fix lint
DNXie e7f5a76
remove xxService class
DNXie 3e733de
simplify launch
DNXie 88dc895
resolve comments
DNXie 82eb6ca
revert shutdown
DNXie 691344c
remove shutdown patch
DNXie d67e55e
support args
DNXie 645dea4
update docstring
DNXie 0862965
support args in as_service
DNXie 48d70e2
fix ci
DNXie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -49,6 +49,7 @@ def options( | |
cls: Type[T], | ||
*, | ||
service_config: ServiceConfig | None = None, | ||
process_config: ProcessConfig | None = None, | ||
num_replicas: int | None = None, | ||
procs_per_replica: int | None = None, | ||
**service_kwargs, | ||
|
@@ -57,7 +58,11 @@ def options( | |
Returns a subclass of this ForgeActor with a bound ServiceConfig. | ||
The returned subclass can later be launched via `.as_service()`. | ||
|
||
Usage (choose ONE of the following forms): | ||
Usage modes: | ||
|
||
---- Service Mode (default) ---- | ||
Use when deploying a replicated service (multiple replicas, each with N procs). | ||
|
||
# Option A: construct ServiceConfig implicitly | ||
DNXie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
service = await MyForgeActor.options( | ||
num_replicas=1, | ||
|
@@ -73,10 +78,42 @@ def options( | |
# Option C: skip options, use the default service config with num_replicas=1, procs_per_replica=1 | ||
service = await MyForgeActor.as_service(...) | ||
await service.shutdown() | ||
|
||
---- Single Actor Mode ---- | ||
Use when launching just one actor directly (without Service abstraction). | ||
Must provide a ProcessConfig. | ||
|
||
cfg = ProcessConfig(...) | ||
actor = await MyForgeActor.options(process_config=cfg).as_actor(...) | ||
await actor.shutdown() | ||
|
||
---- Notes ---- | ||
- If `process_config` is passed, we bind to an actor configuration | ||
and expect `.as_actor(...)` to be called later. | ||
- Otherwise (default), we bind to a service configuration and expect | ||
`.as_service(...)` to be called later. | ||
- Passing both `service_config` and `process_config` is invalid. | ||
""" | ||
|
||
if service_config is not None: | ||
if service_config is not None and process_config is not None: | ||
raise ValueError( | ||
"Cannot pass both `service_config` and `process_config`. " | ||
"Use either `service_config` for service mode or `process_config` for single actor mode." | ||
) | ||
|
||
if process_config is not None: | ||
return type( | ||
f"{cls.__name__}Actor", | ||
(cls,), | ||
{"_process_config": process_config}, | ||
DNXie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
elif service_config is not None: | ||
cfg = service_config | ||
return type( | ||
f"{cls.__name__}Service", | ||
(cls,), | ||
{"_service_config": cfg}, | ||
) | ||
else: | ||
if num_replicas is None or procs_per_replica is None: | ||
raise ValueError( | ||
|
@@ -88,11 +125,11 @@ def options( | |
**service_kwargs, | ||
) | ||
|
||
return type( | ||
f"{cls.__name__}Service", | ||
(cls,), | ||
{"_service_config": cfg}, | ||
) | ||
return type( | ||
f"{cls.__name__}Service", | ||
DNXie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
(cls,), | ||
{"_service_config": cfg}, | ||
) | ||
|
||
@classmethod | ||
async def as_service(cls: Type[T], **actor_kwargs) -> "ServiceInterface": | ||
|
@@ -180,6 +217,23 @@ async def launch(cls, *, process_config: ProcessConfig, **kwargs) -> "ForgeActor | |
await actor.setup.call() | ||
return actor | ||
|
||
@classmethod | ||
async def as_actor(cls: Type[T], **actor_kwargs) -> T: | ||
""" | ||
Spawns a single actor using the ProcessConfig bound in `.options()`. | ||
Example: | ||
cfg = ProcessConfig(...) | ||
actor = await MyForgeActor.options(process_config=cfg).as_actor(...) | ||
""" | ||
cfg = getattr(cls, "_process_config", None) | ||
if cfg is None: | ||
raise ValueError( | ||
"No process_config found. Use `.options(process_config=...)` before calling `.as_actor()`." | ||
) | ||
logger.info("Spawning single actor %s", cls.__name__) | ||
actor = await cls.launch(process_config=cfg, **actor_kwargs) | ||
|
||
return actor | ||
|
||
@classmethod | ||
async def shutdown(cls, actor: "ForgeActor"): | ||
"""Shuts down an actor. | ||
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.