How to run an infinite loop in background and stop it?
Question: 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.

For now, I call my code like that: in gui.py

    
    
    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()
    

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.

I did a bit of research on the question and found :

[Is there any way to kill a Thread in
Python?](http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-
thread-in-python) along with [How to run a function in the background of
tkinter](http://stackoverflow.com/questions/5048082/how-to-run-a-function-in-
the-background-of-tkinter) and [How to run and stop an infinite loop in a
python thread](https://stackoverflow.com/questions/15847717/how-to-run-and-
stop-an-infinite-loop-in-a-python-thread)

The first is promising, but I don't fully understand it, I'm working on it.

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).

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.

Edit: To give more info about the script launched in the thread : (it is a
GNURadio script)

    
    
    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()
    


Answer: Since there is no interaction between your Tkinter gui application and the
main code you're calling from the `sniffer_wideband_09` module I would
recommend you to use multiprocessing:

    
    
    import sniffer_wideband_v09
    import multiprocessing
    from Tkinter import *
    root = Tk()
    def handle_click():
        global t
    
        t = multiprocessing.Process(target=sniffer_wideband_v09.main, args=(sampling_rates, center_frequencies, gains, file_dir, files_names))
        t.start()
    root.mainloop()
    

When you want to stop this process just call `t.terminate()`. Read more on the
multiprocessing module documentation.

