That's one of the "disadvantages" of threads: you cannot safely stop a
thread. Of course you could try, but that's never a good idea. The
reason for this is that threads share memory. They might be holding
locks that, if killed, will never be unlocked. They might (partially)
modify the shared state observed by other threads in such a way that
it becomes unusable to other threads.

So... if you want to kill a thread, I'm sorry to say this: you will
have to bring down the whole process, there's really no other way, and
that's not Python-specific, this is just the design of threads.

On Wed, Sep 25, 2024 at 7:26 PM marc nicole via Python-list
<python-list@python.org> 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?
>
> Thanks!
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to