In Python, you simply cannot kill Thread.
If you really don't need to have Thread (!), What you can do, instead of using a streaming package ( http://docs.python.org/2/library/threading.html ), is to use a multiprocessing package ( http: / /docs.python.org/2/library/multiprocessing.html ). To kill a process, you can simply call the method:
yourProcess.terminate()
Python will kill your process (on Unix through a SIGTERM signal, while on Windows through a call to TerminateProcess ()). Pay attention to use it when using a queue or pipe! (this may damage the data in the queue / handset)
Note that the .Event multiprocessing process is also multiprocessing. Semaphores work just like threading.Event and threading.Semaphore respectively. In fact, the former are clones of the latter.
If you really need to use Thread, it is not possible to directly kill your threads. However, you can use the "daemon stream". In fact, in Python, Thread can be marked as a daemon:
yourThread.daemon = True # set the Thread as a "daemon thread"
The main program will exit if there is not a single living non-demon left. In other words, when your main thread (which, of course, a non-daemon thread) finishes its work, the program will exit, even if there are still some daemon threads.
Note that before calling the start () method, you must set Thread as a daemon!
Of course, you can and should use the daemon even with multiprocessing. Here, when the main process ends, it tries to terminate all of its demonic child processes.
Finally, note that sys.exit () and os.kill () are not choices.