ray.cancel(object_ref: ray.ObjectRef, *, force: bool = False, recursive: bool = True)[source]#

Cancels a task according to the following conditions.

If the specified task is pending execution, it will not be executed. If the task is currently executing, the behavior depends on the force flag. When force=False, a KeyboardInterrupt will be raised in Python and when force=True, the executing task will immediately exit. If the task is already finished, nothing will happen.

Only non-actor tasks can be canceled. Canceled tasks will not be retried (max_retries will not be respected).

Calling ray.get on a canceled task will raise a TaskCancelledError or a WorkerCrashedError if force=True.

  • object_ref – ObjectRef returned by the task that should be canceled.

  • force – Whether to force-kill a running task by killing the worker that is running the task.

  • recursive – Whether to try to cancel tasks submitted by the task specified.


TypeError – This is also raised for actor tasks.

PublicAPI: This API is stable across Ray releases.