title: how to break while loop from singleton class
body: <p>I have a class whose function is called from <code>views.py</code> file in Django app.</p>
  <p><code>views.py</code></p>
  <pre><code>def run_bot(request):
      if request.method == 'POST'
          curr_user = request.user
          bot = Bot(bot_data, user_detail)
          bot.run_status = 'RUN'
          bot.runningBot(waiting_hours, curr_user)
  
  def stop_bot(request):
      if request.method == 'POST':
          data1 = 1
          data2 = 2
          curr_user = request.user
          waiting_hours = int(request.POST['time'])
          bot = Bot(data2, data1)
          bot.run_status = 'STOP'
  </code></pre>
  <p>I am using the Django app to run a selenium bot. There are two buttons in the Front end UI: <code>Run</code> and <code>Stop</code>. When I click the <code>Run</code> button, the <code>POST</code> request is sent through ajax and comes to the function in <code>views.py</code> i.e. <code>run_bot()</code>. Similarly when I press <code>Stop</code> button, the <code>POST</code> request comes through <code>stop_bot()</code> function. Bot these function calls a class and their methods. I created a singleton class so that when a class instance is called from the <code>stop_bot()</code> function, it is should same as being called by the <code>run_bot()</code> function inorder to break a <code>while</code> loop.</p>
  <p><code>singleton class</code></p>
  <pre><code>class Singleton(type):
      _instances = {}
      def __call__(cls, *args, **kwargs):
          if cls not in cls._instances:
              cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
          return cls._instances[cls]
  
  class Bot(metaclass=Singleton):
  
      def __init__(self, bot_data, user_detail):
         self.run_status = ' '
         ...
         ...
  
      def runningBot(self, waiting_hours, curr_user):
  
          no_loops = 0
          while True:
              if self.run_status == 'RUN':
                  browser = self.initializeDriver()
                  time.sleep(2)
                  self.user_login(browser)
                  time.sleep(2)
                  self.performActions(no_loops, curr_user, browser)
  
                  no_loops += 1
                  browser.quit()
                  time.sleep(waiting_hours)
                  # time.sleep(3600 * waiting_hours)
  
              elif self.run_status == 'STOP':
                  break
  </code></pre>
  <p>When <code>Stop</code> is clicked it should stop the loop when the current loop ends. But it isnt stopping. What should be done?</p>