title: Kubernetes python client: Response issue over threading
body: <p>I'm trying to get a list of unhealthy pods in the Kubernetes cluster and then trying to watch the events for message, put them to a list and report. However, I'm not able to get response back for all the threads. The response is pretty unusual, everytime I run the script for some pods or the other I don't get any response back. I'm suspecting there is something wrong with the threads as doing the same over a loop just works fine. Please suggest. </p>
  
  <p><strong>Code:</strong> </p>
  
  <pre><code>#!/usr/bin/env python
  
  import os
  import threading
  import time 
  from texttable import Texttable
  from kubernetes import client, config, watch
  
  #Set config file
  os.environ['KUBECONFIG'] = '/etc/kubernetes/admin.conf'
  
  def get_unhealthy_pods():
      config.load_kube_config()
      try:
          v1 = client.CoreV1Api()
          field_selector = 'status.phase='+'Pending'
          pod_list_response = v1.list_pod_for_all_namespaces(watch=False,field_selector=field_selector)
      except ApiException as e:
          print("Exception when calling CoreV1Api-&gt;list_pod_for_all_namespaces: %s\n" % e)
      pod_dict = {}
      pod_keys = []
      pod_values = []
      for i in pod_list_response.items:
          #print("%s\t%s\t%s" % (i.status.phase, i.metadata.namespace, i.metadata.name))
          # pod_dict[key].append(i.metadata.name)
          # pod_dic[value].append(i.metadata.namespace)
          pod_keys.append(i.metadata.name)
          pod_values.append(i.metadata.namespace)
      pod_dict = dict(zip(pod_keys, pod_values))
      return pod_dict
  
  def get_event_message(pod,namespace):
      config.load_kube_config()
      try:
          v1 = client.CoreV1Api()
          field_selector='involvedObject.name='+pod
          stream = watch.Watch().stream(v1.list_namespaced_event, namespace, field_selector=field_selector, timeout_seconds=10)
      except ApiException as e:
          print("Exception when calling CoreV1Api-&gt;list_namespaced_event: %s\n" % e)
      event_list = []
      iter = 3 
      for i in range(iter): 
          for event in stream:
              event_list.append(event['object'].message)
              print(pod, namespace)
          time.sleep(30)
      events = '\n \n'.join(event_list)
      t = Texttable()
      t.add_row(['Pod', 'Namespace', 'Events'])
      t.add_row([pod, str(namespace), events])
      print(t.draw())
      print("")
      # print(pod, namespace, event_list)
      # print("")
  
  def create_event_histogram():
      unhealthy_pod_dict = get_unhealthy_pods()
      #print(unhealthy_pod_dict)
      thread_count = len(unhealthy_pod_dict)
      print("Total threads: " + str(thread_count))
      thread_list = []
      for i in range(thread_count):
          #print(list(unhealthy_pod_dict.keys())[i], list(unhealthy_pod_dict.values())[i])
          thread  = threading.Thread(target=get_event_message, args={list(unhealthy_pod_dict.keys())[i], list(unhealthy_pod_dict.values())[i]})
          thread_list.append(thread)
  
      for thread in thread_list:
          print("Starting Thread: " + str(thread))
          thread.start()
  
      for thread in thread_list:
          thread.join()
          print("Completed Thread: " + str(thread))
  
  
  #Main
  if __name__ == '__main__':
      create_event_histogram()
      print("Done")
  </code></pre>
  
  <p><strong>Output:</strong> </p>
  
  <pre><code>Total threads: 6
  issol1-22761-7f98bcdf8d-nd58t 007
  umsol1-22761-0 007
  sag-b2b-5df7787df9-884hx 1167763323
  sa-25482-b6f5b46d-x85hz 1518580704
  dbcreatejoba630b9958ba954ca0007-m2c6k 190298074
  is-25992-68d6fb8f6d-t4vws 697189153
  Starting Thread: &lt;Thread(Thread-1, initial)&gt;
  entered event message func
  Ended.
  Starting Thread: &lt;Thread(Thread-2, initial)&gt;
  entered event message func
  Ended.
  Starting Thread: &lt;Thread(Thread-3, initial)&gt;
  entered event message func
  Ended.
  Starting Thread: &lt;Thread(Thread-4, initial)&gt;
  entered event message func
  Event: ADDED sa-25482-b6f5b46d-x85hz.15f5a4413c940a9c MountVolume.SetUp failed for volume "my-secret" : secret "my-secret" not found
  Event: ADDED sa-25482-b6f5b46d-x85hz.15f5a5386edd1df6 (combined from similar events): MountVolume.SetUp failed for volume "is-secret-volume" : secret "my-secret" not found
  Ended.
  Starting Thread: &lt;Thread(Thread-5, initial)&gt;
  entered event message func
  Event: ADDED dbcreatejoba630b9958ba954ca0007-m2c6k.15fb80507afe715f MountVolume.SetUp failed for volume "dbmigrationspec" : secret "create-dbsecreta756b5fb4b285483e" not found
  Event: ADDED dbcreatejoba630b9958ba954ca0007-m2c6k.15fb806d117d0b03 Unable to attach or mount volumes: unmounted volumes=[dbmigrationspec], unattached volumes=[dbmigrationspec default-token-2s5dn]: timed out waiting for the condition
  Ended.
  Starting Thread: &lt;Thread(Thread-6, initial)&gt;
  entered event message func
  Ended.
  Completed Thread: &lt;Thread(Thread-1, stopped 140657099073280)&gt;
  Completed Thread: &lt;Thread(Thread-2, stopped 140657099073280)&gt;
  Completed Thread: &lt;Thread(Thread-3, stopped 140657099073280)&gt;
  Completed Thread: &lt;Thread(Thread-4, stopped 140657099073280)&gt;
  Completed Thread: &lt;Thread(Thread-5, stopped 140657099073280)&gt;
  Completed Thread: &lt;Thread(Thread-6, stopped 140657099073280)&gt;
  Done 
  </code></pre>