• How to stop a specific thread in Python 2.7?

    From marc nicole@mk1853387@gmail.com to comp.lang.python on Wed Sep 25 19:24:49 2024
    From Newsgroup: comp.lang.python

    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!
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Wed Sep 25 17:39:37 2024
    From Newsgroup: comp.lang.python

    marc nicole <mk1853387@gmail.com> wrote or quoted:
    I want to know how to kill a specific running thread (say by its id)

    Killing or stopping a thread can cause data corruption and
    unpredictable behavior. It's better to use a more chill
    approach like setting a flag to let the thread know when to
    wrap it up and exit gracefully. If you really need to terminate
    something abruptly, go for a process instead of a thread.


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Thu Sep 26 06:44:09 2024
    From Newsgroup: comp.lang.python

    On 25Sep2024 19:24, marc nicole <mk1853387@gmail.com> wrote:
    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?

    No, `set()` doesn't kill a thread at all. It sets the `Event`, and each
    thread must be checking that event regularly, and quitting if it becomes
    set.

    You just need a per-thred vent instead of a single Event for all the
    threads.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From marc nicole@mk1853387@gmail.com to comp.lang.python on Wed Sep 25 22:56:58 2024
    From Newsgroup: comp.lang.python

    How to create a per-thread event in Python 2.7?

    On Wed, 25 Sept 2024, 22:47 Cameron Simpson via Python-list, < python-list@python.org> wrote:

    On 25Sep2024 19:24, marc nicole <mk1853387@gmail.com> wrote:
    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?

    No, `set()` doesn't kill a thread at all. It sets the `Event`, and each thread must be checking that event regularly, and quitting if it becomes
    set.

    You just need a per-thred vent instead of a single Event for all the
    threads.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>
    --
    https://mail.python.org/mailman/listinfo/python-list

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Thu Sep 26 11:06:03 2024
    From Newsgroup: comp.lang.python

    On 25Sep2024 22:56, marc nicole <mk1853387@gmail.com> wrote:
    How to create a per-thread event in Python 2.7?

    Every time you make a Thread, make an Event. Pass it to the thread
    worker function and keep it to hand for your use outside the thread.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Left Right@olegsivokon@gmail.com to comp.lang.python on Wed Sep 25 22:14:43 2024
    From Newsgroup: comp.lang.python

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114