APIs for accessing runtime context information about the current driver, task, or actor.
Get the runtime context of the current driver/worker.
import ray
from ray import runtime_context
ctx = runtime_context.get_runtime_context()
job_id = ctx.get_job_id()get_job_id(): Get current job IDget_worker_id(): Get current worker IDget_node_id(): Get the ID of the node this process is running onget_node_labels(): Get node labels of the current worker
get_task_id(): Get current task IDget_task_name(): Get current task nameget_task_function_name(): Get current task function name
get_actor_id(): Get current actor ID (only in actor context)get_actor_name(): Get current actor name (only in actor context)current_actor: Get the current actor handle (only in actor context)was_current_actor_reconstructed(): Check if actor has been restarted
get_assigned_resources(): Get resources assigned to this workerget_resource_ids(): Get resource IDsget_accelerator_ids(): Get visible accelerator IDsget_placement_group_id(): Get current placement group IDcurrent_placement_group_id: Get current placement group ID (property)
namespace: Get current namespaceruntime_env: Get runtime environment used for current driver/workerget_runtime_env_string(): Get runtime environment stringgcs_address: Get GCS address of the Ray cluster
get(): Get a dictionary of the current contextshould_capture_child_tasks_in_placement_group(): Check if child tasks should capture parent's placement group
import ray
from ray import runtime_context
@ray.remote
def task():
ctx = runtime_context.get_runtime_context()
return {
"job_id": ctx.get_job_id(),
"task_id": ctx.get_task_id(),
"node_id": ctx.get_node_id()
}
@ray.remote
class Actor:
def get_context(self):
ctx = runtime_context.get_runtime_context()
return {
"actor_id": ctx.get_actor_id(),
"job_id": ctx.get_job_id(),
"was_reconstructed": ctx.was_current_actor_reconstructed()
}