• How to stop a specific thread in Python 2.7?

    From marc nicole@3:633/280.2 to All on Thu Sep 26 03:24:49 2024
    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!

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Thu Sep 26 03:39:37 2024
    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.



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Cameron Simpson@3:633/280.2 to All on Thu Sep 26 06:44:09 2024
    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>

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From marc nicole@3:633/280.2 to All on Thu Sep 26 06:56:58 2024
    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


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Cameron Simpson@3:633/280.2 to All on Thu Sep 26 11:06:03 2024
    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.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Left Right@3:633/280.2 to All on Thu Sep 26 06:14:43 2024
    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=E2=80=AFPM 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 =3D threading.Thread(target=3D self.some_func(), args=3D( ...,), =
    )
    thread1.start()
    # kill the thread
    event_thread1 =3D 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

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)