[Tutor] How to stop a specific thread in Python 2.7?

marc nicole mk1853387 at gmail.com
Wed Sep 25 15:57:48 EDT 2024


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, <mats at wichmann.us> 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  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list