title: How to call a method while another method is still running- Python
body: <p>I'm working on a python program that extracts the sound from a given YouTube video url and plays it. I created a <code>play_vid</code> method to play the sound and a <code>pause_vid</code> method to pause the sound. The issue is that while the sound is still playing inside the <code>play_vid</code> method and I try to call the <code>pause_vid</code> method, the call gets ignored and the <code>play_vid</code> method keeps running. Example:</p>
  <pre><code>vid = Video(&quot;youtubelink&quot;)
  vid.play_vid()
  time.sleep(5)
  # The sound keeps playing/this method call is being ignored
  vid.pause_vid()
  </code></pre>
  <p>How do I terminate the <code>play_vid</code> method when the <code>pause_vid</code> method is called? I've been researching for a while and wasn't able to find a solution to this problem. Full program:</p>
  <pre><code>from selenium import webdriver
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  from selenium.webdriver.common.by import By
  import time, os, vlc, requests
  from mutagen.mp3 import MP3
  
  
  class InvalidUrl(Exception):
      def __init__(self, message=&quot;Invalid video url!&quot;):
          super().__init__(message)
  
  
  class Video:
      def __init__(self, url):
          self.url = url
          self.paused = False
          self.player = &quot;&quot;
          self.is_valid()
          self.time_sleep = 0
  
      def play_vid(self):
          driver = webdriver.Chrome(&quot;C:\chromedriver.exe&quot;)
          driver.get(&quot;https://ytmp3.cc/en14/&quot;)
          element = driver.find_element_by_id(&quot;input&quot;)
          element.send_keys(self.url)
          button = driver.find_element_by_id(&quot;submit&quot;)
          button.click()
          wait = WebDriverWait(driver, 10)
          button2 = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id=&quot;buttons&quot;]/a[1]')))
          button2.click()
          vid_name = driver.find_element_by_xpath('//*[@id=&quot;title&quot;]').text
          vid_name = vid_name.replace(&quot;| &quot;, &quot;&quot;)
          file_name = os.path.expanduser(r&quot;~\Downloads\{}.mp3&quot;.format(vid_name))
  
          while not os.path.exists(file_name):
              time.sleep(1)
  
          driver.close()
          self.player = vlc.MediaPlayer()
          media = vlc.Media(file_name)
          self.player.set_media(media)
          self.player.play()
          audio = MP3(file_name)
          audio_info = audio.info
          length_in_secs = int(audio_info.length)
  
          while self.time_sleep != length_in_secs:
              if self.paused is True:
                  return
  
              time.sleep(1)
              self.time_sleep += 1
  
      def pause_vid(self):
          self.paused = True
  
      def is_valid(self):
          r = requests.get(self.url)
  
          if &quot;Video unavailable&quot; in r.text:
              raise InvalidUrl
  </code></pre>
  <p>A big thanks to Mark M for letting me know that I forgot to add <code>self.player.set_pause(1)</code> in the <code>pause_vid</code> method. He also advised me to remove the while loop, so I used <code>time.sleep</code> in my main program in order to decide how long the video will play for instead of the while loop. Updated code:</p>
  <pre><code>from selenium import webdriver
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  from selenium.webdriver.common.by import By
  import time, os, vlc, requests
  from mutagen.mp3 import MP3
  
  
  class InvalidYouTubeUrl(Exception):
      def __init__(self, message=&quot;Invalid YouTube url!&quot;):
          super().__init__(message)
  
  
  class Video:
      def __init__(self, url):
          self.url = url
          self.player = None
          self.is_valid()
          self.vid_len = None
  
      def play_vid(self):
          driver = webdriver.Chrome(&quot;C:\chromedriver.exe&quot;)
          driver.get(&quot;https://ytmp3.cc/en14/&quot;)
          element = driver.find_element_by_id(&quot;input&quot;)
          element.send_keys(self.url)
          button = driver.find_element_by_id(&quot;submit&quot;)
          button.click()
          wait = WebDriverWait(driver, 10)
          button2 = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id=&quot;buttons&quot;]/a[1]')))
          button2.click()
          vid_name = driver.find_element_by_xpath('//*[@id=&quot;title&quot;]').text
          vid_name = vid_name.replace(&quot;| &quot;, &quot;&quot;)
          file_name = os.path.expanduser(r&quot;~\Downloads\{}.mp3&quot;.format(vid_name))
  
          while not os.path.exists(file_name):
              time.sleep(1)
  
          driver.close()
          self.player = vlc.MediaPlayer()
          media = vlc.Media(file_name)
          self.player.set_media(media)
          self.player.play()
          audio = MP3(file_name)
          audio_info = audio.info
          self.vid_len = int(audio_info.length)
  
      def pause_vid(self):
          self.player.set_pause(1)
  
      def is_valid(self):
          r = requests.get(self.url)
  
          if &quot;Video unavailable&quot; in r.text:
              raise InvalidYouTubeUrl
  </code></pre>
  <p>I've also found a way to play the whole sound in the main program:</p>
  <pre><code>import pyytmp3, time
  
  vid = pyytmp3.Video(&quot;youtubelink&quot;)
  vid.play_vid()
  time.sleep(vid.vid_len)
  </code></pre>
  <p>I'm basically just delaying the program by the length of the video.</p>