Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #196760

Re: How to stop a specific thread in Python 2.7?

From Left Right <olegsivokon@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to stop a specific thread in Python 2.7?
Date 2024-09-25 22:14 +0200
Message-ID <mailman.12.1727363790.2990.python-list@python.org> (permalink)
References <CAGJtH9RBmcofpg5ifiXZm4z8XRBQkGVzDSduR7=9QH75-Ubpgw@mail.gmail.com> <CAJQBtgmtB-Qxv2t+w41nkH6vgTTj9_JV7UEUuQS3hZUc6PBe5A@mail.gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to stop a specific thread in Python 2.7? Left Right <olegsivokon@gmail.com> - 2024-09-25 22:14 +0200

csiph-web