Re: How to stop a specific thread in Python 2.7?
marc nicole via Tutor <[email protected]> Wed, 25 Sep 2024 21:57:48 +0200
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <CAGJtH9Sv9TH36TqUvD44Ondg9rWK+EvXvgseQGENxmmiaJpneA@mail.gmail.com> |
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: The robot should run the thread in the state idle to get values needed for a task, but if the user talks to him and the values weren't captured yet, the robot should run the function again in a blocking manner until he gets the values for the task, while stopping the initial call to it through the thread. On Wed, 25 Sept 2024, 20:27 Mats Wichmann, <[email protected]> wrote: > On 9/25/24 11:24, marc nicole via Tutor wrote: > > Hello guys, > > > > I want to know how to kill a specific running thread (say by its id) > > > > for now I run and kill a thread like the following: > > # start thread > > thread1 = threading.Thread(target= self.some_func(), args=( ...,), ) > > thread1.start() > > # kill the thread > > event_thread1 = threading.Event() > > event_thread1.set() > > > > I know that set() will kill all running threads, but if there was thread2 > > as well and I want to kill only thread1? > There's no official way - as usual we'll come back with a question > (well, I will): Why Would You Want To Do That? Threads often hold > critical-section locks and other important things and killing such a > thread will put you in an undefined, possibly unstable state. That may > not be your case... it would be useful if you described a use case that > causes you to want to kill an individual thread. Or are you just curious? > > > _______________________________________________ > 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