Skip to content

[Docs] Note that class variables aren't shared across actor instances (#12977)#63459

Open
dstrodtman wants to merge 1 commit into
ray-project:masterfrom
dstrodtman:doc-992-actor-class-variables
Open

[Docs] Note that class variables aren't shared across actor instances (#12977)#63459
dstrodtman wants to merge 1 commit into
ray-project:masterfrom
dstrodtman:doc-992-actor-class-variables

Conversation

@dstrodtman
Copy link
Copy Markdown
Contributor

Description

Long-standing clarification request from 2020: users instantiating multiple @ray.remote actors 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 — but doc/source/ray-core/actors.rst only 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

…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>
@dstrodtman dstrodtman requested a review from a team as a code owner May 18, 2026 18:01
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +96 to +106
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`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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).

Copy link
Copy Markdown
Member

@Yicheng-Lu-llll Yicheng-Lu-llll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ref might be pointing to the wrong page. I think you may want to link to the named actor page instead.

@Yicheng-Lu-llll Yicheng-Lu-llll self-assigned this May 18, 2026
@ray-gardener ray-gardener Bot added docs An issue or change related to documentation core Issues that should be addressed in Ray Core labels May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core docs An issue or change related to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[core] Python class variables not updating between all remote instances of the class

2 participants