code: def test_cancel_task(self):
      temp = tempfile.NamedTemporaryFile(delete=True)
      temp.close()
      path = temp.name
  
      def sleep(x):
          import os
          import time
  
          with open(path, "w") as f:
              f.write("%d %d" % (os.getppid(), os.getpid()))
          time.sleep(100)
  
      # start job in background thread
      def run():
          try:
              self.sc.parallelize(range(1), 1).foreach(sleep)
          except Exception:
              pass
  
      import threading
  
      t = threading.Thread(target=run)
      t.daemon = True
      t.start()
  
      daemon_pid, worker_pid = 0, 0
      cnt = 0
      while True:
          if os.path.exists(path):
              with open(path) as f:
                  data = f.read().split(" ")
              try:
                  daemon_pid, worker_pid = map(int, data)
              except ValueError:
                  # In case the value is not written yet.
                  cnt += 1
                  if cnt == 10:
                      raise
              else:
                  break
          time.sleep(1)
  
      # cancel jobs
      self.sc.cancelAllJobs()
      t.join()
  
      for i in range(50):
          try:
              os.kill(worker_pid, 0)
              time.sleep(0.1)
          except OSError:
              break  # worker was killed
      else:
          self.fail("worker has not been killed after 5 seconds")
  
      try:
          os.kill(daemon_pid, 0)
      except OSError:
          self.fail("daemon had been killed")
  
      # run a normal job
      rdd = self.sc.parallelize(range(100), 1)
      self.assertEqual(100, rdd.map(str).count())
  
imports: import os
  import sys
  import tempfile
  import threading
  import time
  import unittest
  from py4j.protocol import Py4JJavaError
  from pyspark import SparkConf, SparkContext
  from pyspark.testing.utils import ReusedPySparkTestCase, PySparkTestCase, QuietTest, eventually
label_desc: Use image node_id 1 for calling the WorkerTests obj's underlying member method code with example usage: obj.test_cancel_task() without return types