I have a program with two threaded processes that run on a loop. The problem is, I'm new enough to python that outside of closing the terminal window, I don't know the best method to properly terminate the application.
Asked
Active
Viewed 89 times
1
-
Two processes that use threads? Or one process that has two threads? – Colonel Thirty Two Jul 08 '15 at 15:15
-
related: http://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python?rq=1 – WorldSEnder Jul 08 '15 at 15:16
-
probably isn't worded right. I have one function running on two separate threads. The function has an infinite loop. There's a bit more to it, because on the surface this sounds like it's only goal is to build ineffeciency, but this is the jist of it – John Sly Jul 08 '15 at 15:17
-
Are you on a Linux machine? Always good to learn terminal commands. https://www.digitalocean.com/community/tutorials/how-to-use-ps-kill-and-nice-to-manage-processes-in-linux – qzcx Jul 08 '15 at 15:20
-
You can make use of the `join()` method that comes with the created thread (assuming that you used the `threading` module). – Some guy Jul 08 '15 at 15:43
-
Best way? Or just the quickest way? The best way is to send a signal using an `Event()` or similar object to let the threads know to terminate so that they can correctly release any resources before terminating. – Joel Cornett Jul 08 '15 at 16:00
-
@JohnSly It has come to light that further clarifications are needed in your question. Like if you are using the Python "threading" module or not. Or whether it is important to have these threads have the ability to clean up their resources, or not. (Generally it is a good idea). The general consensus is that more code will be added to each thread for safety over expedience. You are cool to that, right? – rocky Jul 08 '15 at 16:21
-
I'm using the threading module. Currently I've just been terminating with ctrl+c. it isn't really elegant, but it hasn't mattered too much given the small scale of my application. Once I added threading, it would no longer terminate, which was obviously an issue. – John Sly Jul 08 '15 at 20:07
1 Answers
1
The best way to terminate any threaded application, including python, is a technique known as "cooperative shutdown". With this technique, in each thread you check if the application has been instructed to shutdown on each loop iteration, and if so, exit the loop and finish running each thread. Your shutdown condition is completely up to you but common options include catching a KeyboardInterrupt exception and then setting a shared shutdown variable, timeout, etc, etc...

Andy
- 1,663
- 10
- 17
-
Thanks, that's the clearest way you could have explained it. It gives me a little more to look into for best practices that'll fit my needs too, but you did sum it up really well. – John Sly Jul 08 '15 at 20:06
-
True @MartinJames, but if you can it should be designed in at the forefront. Most cases I've seen where it's not possible are due to developers threading with absolutely no thought given to shutdown until it's way to late. – Andy Jul 09 '15 at 18:59