Skip to content

[tune] Trainable functions cannot execute Ray methods since Ray is not thread safe #1851

@ericl

Description

@ericl

Describe the problem

The following snippet will crash with

ray.tune.error.TuneError: Error running trial: The Ray methods are not thread safe and must be called from the main thread. This method was called from thread Thread-2.
import ray
from ray.tune import run_experiments, register_trainable


ray.init()

def my_train(config, reporter):  # add the reporter parameter
    ray.put("HI")

ray.tune.register_trainable("my_train", my_train)


ray.tune.run_experiments({
    "my_experiment": {
        "run": "my_train",
    }
})

This is since Tune has to run long-running functions off of the main thread. This prevents tune users from interacting with Ray when using the trainable functions interface.

One workaround is to define the trainable as a Trainable class, which tune does run on the main thread since train() yields control back to the tune periodically for returning results.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions