Get current task ID for this worker. 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: .. testcode:: import ray @ray.remote class Actor:
(self)
| 228 | return task_id if not task_id.is_nil() else None |
| 229 | |
| 230 | def get_task_id(self) -> Optional[str]: |
| 231 | """Get current task ID for this worker. |
| 232 | |
| 233 | Task ID is the id of a Ray task. The ID will be in hex format. |
| 234 | This shouldn't be used in a driver process. |
| 235 | |
| 236 | Example: |
| 237 | |
| 238 | .. testcode:: |
| 239 | |
| 240 | import ray |
| 241 | |
| 242 | @ray.remote |
| 243 | class Actor: |
| 244 | def get_task_id(self): |
| 245 | return ray.get_runtime_context().get_task_id() |
| 246 | |
| 247 | @ray.remote |
| 248 | def get_task_id(): |
| 249 | return ray.get_runtime_context().get_task_id() |
| 250 | |
| 251 | # All the below code generates different task ids. |
| 252 | a = Actor.remote() |
| 253 | # Task ids are available for actor tasks. |
| 254 | print(ray.get(a.get_task_id.remote())) |
| 255 | # Task ids are available for normal tasks. |
| 256 | print(ray.get(get_task_id.remote())) |
| 257 | |
| 258 | .. testoutput:: |
| 259 | :options: +MOCK |
| 260 | |
| 261 | 16310a0f0a45af5c2746a0e6efb235c0962896a201000000 |
| 262 | c2668a65bda616c1ffffffffffffffffffffffff01000000 |
| 263 | |
| 264 | Returns: |
| 265 | The current worker's task id in hex. None if there's no task id. |
| 266 | """ |
| 267 | # only worker mode has task_id |
| 268 | if self.worker.mode != ray._private.worker.WORKER_MODE: |
| 269 | logger.warning( |
| 270 | "This method is only available when the process is a " |
| 271 | f"worker. Current mode: {self.worker.mode}" |
| 272 | ) |
| 273 | return None |
| 274 | task_id = self._get_current_task_id() |
| 275 | return task_id.hex() if not task_id.is_nil() else None |
| 276 | |
| 277 | def _get_current_task_id(self) -> TaskID: |
| 278 | return self.worker.current_task_id |
no test coverage detected