[Docs] Note that class variables aren't shared across actor instances (#12977)#63459
[Docs] Note that class variables aren't shared across actor instances (#12977)#63459dstrodtman wants to merge 1 commit into
Conversation
…ray-project#12977) Long-standing clarification request: Python class variables on a ``@ray.remote`` class aren't shared between actor instances because each actor runs in its own Python process. Add a note next to the actor instantiation section in ``doc/source/ray-core/actors.rst`` explaining this and pointing users at named actors or ``ray.put`` for shared state. Closes ray-project#12977 [DOC-992] Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Signed-off-by: Douglas Strodtman <douglas@anyscale.com>
There was a problem hiding this comment.
Code Review
This pull request adds a note to the documentation explaining actor process isolation and the behavior of class variables across actor instances. The review feedback suggests generalizing the language to include other supported languages like Java, as the isolation principle is not exclusive to Python, and provides a revised version of the text to improve clarity for all users.
| Each remote actor runs in its own Python process, so Python class | ||
| variables aren't shared across actor instances. Each actor receives its | ||
| own copy of the class state at instantiation time, and any subsequent | ||
| mutations (for example, ``Counter.value += 1`` from inside a method) | ||
| are local to that actor's process. They aren't visible to the driver | ||
| or to other actors created from the same class. | ||
|
|
||
| To share state across actors, store it on a dedicated actor (often a | ||
| :ref:`named actor <actor-lifetimes>`) and call its methods from the | ||
| other actors, or place immutable data into the object store with | ||
| :func:`ray.put`. |
There was a problem hiding this comment.
The note is placed in a general section that follows a multi-language tab-set (Python, Java, C++), but the wording is currently Python-specific (e.g., "Python process", "Python class variables", and the reference to :func:ray.put). Since the principle of process isolation applies to all Ray languages (e.g., static fields in Java are also not shared across actors), it would be more consistent to use language-agnostic terminology or provide examples for other languages to ensure the note is clear for all users. Additionally, the example Counter.value += 1 refers to a class variable, while the Counter example provided in the code blocks above uses an instance variable self.value; clarifying this distinction would help avoid confusion.
| Each remote actor runs in its own Python process, so Python class | |
| variables aren't shared across actor instances. Each actor receives its | |
| own copy of the class state at instantiation time, and any subsequent | |
| mutations (for example, ``Counter.value += 1`` from inside a method) | |
| are local to that actor's process. They aren't visible to the driver | |
| or to other actors created from the same class. | |
| To share state across actors, store it on a dedicated actor (often a | |
| :ref:`named actor <actor-lifetimes>`) and call its methods from the | |
| other actors, or place immutable data into the object store with | |
| :func:`ray.put`. | |
| Each remote actor runs in its own process, so class variables (such as | |
| Python class variables or Java static fields) aren't shared across actor | |
| instances. Each actor receives its own copy of the class state at | |
| instantiation time, and any subsequent mutations (for example, | |
| ``Counter.value += 1`` in Python) are local to that actor's process. | |
| They aren't visible to the driver or to other actors created from the | |
| same class. | |
| To share state across actors, store it on a dedicated actor (often a | |
| :ref:`named actor <actor-lifetimes>`) and call its methods from the | |
| other actors, or place immutable data into the object store (for example, | |
| using :func:`ray.put` in Python). |
Yicheng-Lu-llll
left a comment
There was a problem hiding this comment.
Thank you for raising the pr, just left some nits.
| To share state across actors, store it on a dedicated actor (often a | ||
| :ref:`named actor <actor-lifetimes>`) and call its methods from the | ||
| other actors, or place immutable data into the object store with | ||
| :func:`ray.put`. |
There was a problem hiding this comment.
I'm not sure if ray.put is the recommended approach here, since the object is immutable, every time you update it, you'd need to create a new one.
| or to other actors created from the same class. | ||
|
|
||
| To share state across actors, store it on a dedicated actor (often a | ||
| :ref:`named actor <actor-lifetimes>`) and call its methods from the |
There was a problem hiding this comment.
This ref might be pointing to the wrong page. I think you may want to link to the named actor page instead.
Description
Long-standing clarification request from 2020: users instantiating multiple
@ray.remoteactors observe that a Python class variable doesn't accumulate across instances. The cause is process isolation — each actor runs in its own Python process and gets its own copy of the class state — butdoc/source/ray-core/actors.rstonly mentions that fact obliquely (once on the introductory line). This PR adds a short note next to the actor instantiation section that states the behavior explicitly and points users at named actors or the object store for shared state.Related issues
Closes #12977
[DOC-992]
Additional information
Target file:
doc/source/ray-core/actors.rst. The note is placed immediately after the language tab-set that introduces actor instantiation so it's visible regardless of which language tab the reader selects.Generated with Claude Code