Re: How to stop a specific thread in Python 2.7?
"avi.e.gross--- via Tutor" <[email protected]> Wed, 25 Sep 2024 20:50:56 -0400
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
There is a low tech approach if you are willing to let the thread run a little longer or use up some time checking. I see others suggesting similar things. There are many ways other parts of your program can set a flag that the thread can occasionally check and then quit. Some are as simple as creating a temporary file with a known name and the thread would check periodically and when found, delete it and quit. And, of course, various methods can be used for a sort of messaging in memory. But as noted by Mats, the best kill is suicide after settling your affairs. In the real world, I suspect this is a solved problem. Imagine for example if you decide to simultaneously attack a problem in multiple ways and the first one to finish means the others need not continue. An example might be trying to determine what language a paragraph is in that will start threads checking to see if it matches one or another language and returning if the likelihood is very high. -----Original Message----- From: Tutor <[email protected]> On Behalf Of Mats Wichmann Sent: Wednesday, September 25, 2024 4:27 PM To: marc nicole <[email protected]> Cc: [email protected] Subject: Re: [Tutor] How to stop a specific thread in Python 2.7? On 9/25/24 13:57, marc nicole wrote: > Why i want to kill a thread? > > At first, i want to look for two values through a thread, if i got them > before an event happens, that's cool, the thread will end itself and i > will use the values in the subsequent code, if not i want to kill the > background running function/thread, and relaunch the function of thread > (blocking call this time) to get the values and then continue the program. > > The context is robotics: So to start out, from the documentation: "currently, there are no priorities, no thread groups, and threads cannot be destroyed, stopped, suspended, resumed, or interrupted." (see the bits just above this heading: https://docs.python.org/3.12/library/threading.html#thread-local-data) So you need to find a way to extend your model to the negative case as well, so the thread stops itself even if the ideal case didn't occur. I think one approach is to use Event, as you've already started to think about, I'd just change the naming to be more clear: should_stop = threading.Event() # then at some point: should_stop.set() and in your thread: while not should_stop.wait(1): # do whatever work you can do I'm guessing you've already been here and this isn't new info. Maybe someone else has more experience... From stuff I've heard, robotics folks are making use of async I/O to solve these situations, rather than threads. Unfortunately, Python's async is a bit of a pain as it's kind of like a virus: a function that calls async routines must itself be async, and so on... it kind of spreads throughout your program this way, and can be a lot of work unless you built it from scratch as async. _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor