title: DeadLock in Python
body: <p>I'm programing a GUI with <code>tkinter</code> that uses <code>cv2</code> to extract the video stream from the computer camera. To do that, a <code>Thread</code> reloads the image using a while loop.</p>
  
  <p>I have been using <code>Locks</code> so both <code>Threads</code> (the main one and the auxiliar) end their process as expected. However, i'm not able to do that because of a DeadLock that I can't identify.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>import tkinter as tk
  import cv2
  import time
  import sys
  import threading
  from PIL import ImageTk, Image
  
  
  class Application:
      gui_root = None
      gui_panel = None
  
      frame = None
      video_capture = None
      stop_event = None
      thread = None
      thread_lock = None
  
      def __init__(self):
  
          self.gui_root = tk.Tk()
          self.gui_root.geometry("640x480")
          self.gui_root.resizable(False, False)
  
          self.video_capture = cv2.VideoCapture(0)
  
          self.stop_event = threading.Event()
          self.thread_lock = threading.Lock()
          self.thread = threading.Thread(target=self.load_frame, args=())
          self.thread.start()
  
          self.gui_root.wm_protocol("WM_DELETE_WINDOW", self.on_close)
  
      def start(self):
  
          self.gui_root.mainloop()
  
      def load_frame(self):
  
          print('Starting load_frame')
  
          while self.thread_lock.acquire() and not self.stop_event.is_set():
  
                  _, self.frame = self.video_capture.read()
                  self.frame = cv2.resize(self.frame, (640, 480))
  
                  c_image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
                  c_image_tk = ImageTk.PhotoImage(Image.fromarray(c_image))
  
                  if self.gui_panel is None:
                      self.gui_panel = tk.Label(image=c_image_tk)
                      self.gui_panel.place(width=640, height=480, x=0, y=0)
                  else:
                      self.gui_panel.configure(image=c_image_tk)
  
                  self.thread_lock.release()
  
          print("End of while")
          self.thread_lock.release()
          print('End of Process 2')
  
      def on_close(self):
  
          self.stop_event.set()
          print("Event set")
          with self.thread_lock:
              self.video_capture.release()
              self.gui_root.destroy()
  
  
  app = Application()
  app.start()
  </code></pre>