code: def test_worker_kill(ray_start_4_cpus, backend):
      if backend == "test":
          test_config = BackendConfig()
      elif backend == "torch":
          from ray.train.torch import TorchConfig
  
          test_config = TorchConfig()
      elif backend == "tf":
          from ray.train.tensorflow import TensorflowConfig
  
          test_config = TensorflowConfig()
      elif backend == "horovod":
          from ray.train.horovod import HorovodConfig
  
          test_config = HorovodConfig()
  
      def train_func():
          for i in range(2):
              train.report(dict(loss=1, iter=i))
  
      iterator = create_iterator(train_func, test_config)
      kill_callback = KillCallback(
          fail_on=0, backend_executor=iterator._backend_executor
      )
  
      for intermediate_result in iterator:
          # Run 1: iter=0, counter=1, Successful
          # Run 2: iter=1, counter=1, Unsuccessful, starts training from beginning
          # Run 3: iter=0, counter=2, Successful
          # Run 4: iter=1, counter=3, Successful
          kill_callback.handle_result()
      assert kill_callback.counter == 3
  
      iterator = create_iterator(train_func, test_config)
      kill_callback = KillCallback(
          fail_on=1, backend_executor=iterator._backend_executor
      )
      for intermediate_result in iterator:
          # Run 1: iter=0, counter=1, Successful
          # Run 2: iter=1, counter=2, Successful
          # Run 3: None, counter=2, Unsuccessful, starts training from beginning.
          # Run 4: iter=0, counter=3, Successful
          # Run 5: iter=1, counter=4, Successful
          kill_callback.handle_result()
      assert kill_callback.counter == 4
  
imports: import functools
  import time
  from unittest.mock import patch
  import pytest
  import ray
  from ray import train
  from ray.air._internal.util import StartTraceback
  from ray.train import DataConfig
  from ray.train._internal.backend_executor import BackendExecutor
  from ray.train._internal.session import get_session, init_session
  from ray.train._internal.utils import construct_train_func
  from ray.train._internal.worker_group import WorkerGroup
  from ray.train.backend import BackendConfig
  from ray.train.examples.pytorch.torch_linear_example import train_func
  from ray.train.examples.tf.tensorflow_mnist_example import train_func
  from ray.train.tests.util import mock_storage_context
  from ray.train.trainer import TrainingIterator
label_desc: Use image node_id 14 for calling a global function with example usage: test_worker_kill(ray_start_4_cpus, backend) without return types