title: How to run an infinite loop in background and stop it?
body: <p>Here is my problem: Using Tkinter, I want to click a button and launch a python script.
  This python script is now a module (I don't know if it s the best way to do it) imported to my main script.
  This script should be running in background. There is a method in it to quit it. How can I call it ?
  I wanted to pass a flag or something to the module but I don't know how to do that.</p>
  
  <p>For now, I call my code like that: in gui.py</p>
  
  <pre><code>import sniffer_wideband_v09
  from Tkinter import *
  root = Tk()
  def handle_click():
      global t
      global stop_flag
      stop_flag = 0
  
      def callback():
          sniffer_wideband_v09.main(sampling_rates, center_frequencies, gains, file_dir, files_names) 
      t = Thread(target=callback)
      t.start()
  root.mainloop()
  </code></pre>
  
  <p>I would like to call the quitting() method in sniffer_wideband_v09.py. Or to pass a flag to my module to stop the infinite loop. 
  Afterward, I will need to bind all of this to Tkinter Buttons.</p>
  
  <p>I did a bit of research on the question and found :</p>
  
  <p><a href="https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python">Is there any way to kill a Thread in Python?</a>
  along with
  <a href="https://stackoverflow.com/questions/5048082/how-to-run-a-function-in-the-background-of-tkinter">How to run a function in the background of tkinter</a>
  and 
  <a href="https://stackoverflow.com/questions/15847717/how-to-run-and-stop-an-infinite-loop-in-a-python-thread">How to run and stop an infinite loop in a python thread</a></p>
  
  <p>The first is promising, but I don't fully understand it, I'm working on it.</p>
  
  <p>Note: I run it directly from my shell with ./gui.py and I am under Ubuntu, not windows.(I think it can change some ways of dealing with multi threading). </p>
  
  <p>Thanks for the reading, any hint or help will be appreciated. I will post my code if I find a response in the mean time. </p>
  
  <p>Edit: To give more info about the script launched in the thread : (it is a GNURadio script)</p>
  
  <pre><code>class foo():
      def __init__(self):
           self.parameters()
      def methods():
           self.dostuff()
  def main(sampling_rates, center_frequencies, gains, file_dir, files_names):
      tb = foo()
      while True: #flag could be here to exit the infinite while.
           tb.start()
      tb.stop()
  def quitting():
      tb.stop()
   ## not mandatory piece of code from now on
  if __name = "__main__":
       main()
       quitting()
  </code></pre>