get_world_size#

TuneContext.get_world_size() → int[source]#

Get the current world size (i.e. total number of workers) for this run.

Examples

>>> import ray
>>> from ray import train
>>> from ray.train import ScalingConfig
>>> from ray.train.tensorflow import TensorflowTrainer
>>> NUM_WORKERS = 2
>>> def train_loop_per_worker(config):
...     assert train.get_context().get_world_size() == NUM_WORKERS
>>> train_dataset = ray.data.read_csv("s3://anonymous@ray-example-data/iris.csv")
>>> trainer = TensorflowTrainer(
...     train_loop_per_worker,
...     scaling_config=ScalingConfig(num_workers=NUM_WORKERS),
...     datasets={"train": train_dataset}
... )
>>> print("[ignore]"); trainer.fit()
[ignore]...
Returns:

The total number of workers, or 1 if called outside a Ray Train or Ray Tune function.

Return type:

int

PublicAPI (beta): This API is in beta and may change before becoming stable.

Warning

DEPRECATED: This API is deprecated and may be removed in future Ray releases. get_world_size is deprecated for Ray Tune because there is no concept of worker ranks for Ray Tune, so these methods only make sense to use in the context of a Ray Train worker. See this issue for more context and migration options: ray-project/ray#49454. Disable these warnings by setting the environment variable: RAY_TRAIN_ENABLE_V2_MIGRATION_WARNINGS=0