ray.runtime_context.RuntimeContext.get_task_id#

RuntimeContext.get_task_id() str | None[source]#

Get current task ID for this worker or driver.

Task ID is the id of a Ray task. The ID will be in hex format. This shouldn’t be used in a driver process.

Example

import ray

@ray.remote
class Actor:
    def get_task_id(self):
        return ray.get_runtime_context().get_task_id()

@ray.remote
def get_task_id():
    return ray.get_runtime_context().get_task_id()

# All the below code generates different task ids.
a = Actor.remote()
# Task ids are available for actor tasks.
print(ray.get(a.get_task_id.remote()))
# Task ids are available for normal tasks.
print(ray.get(get_task_id.remote()))
16310a0f0a45af5c2746a0e6efb235c0962896a201000000
c2668a65bda616c1ffffffffffffffffffffffff01000000
Returns:

The current worker’s task id in hex. None if there’s no task id.