Python Forum

Full Version: how to delete thread
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I keep fixing the script after I ask for help its embarrassing Cry Cry
how do I delete a thread
(Apr-27-2025, 04:19 PM)Azdaghost Wrote: [ -> ]how do I delete a thread
we don't delete threads
It's possible, but it is not exposed. It's not exposed, because it locks up the interpreter.


Here is a way to end a thread. The Event object is used, to communicate thread safe:
import time

from threading import Event, Thread, Timer


def worker(event):
    while not event.is_set():
        print("Ping")
        time.sleep(1)


def main():
    event = Event()
    worker_thread = Thread(target=worker, args=(event,))
    worker_thread.start()

    # set event after 5 seconds
    Timer(5, event.set).start() # timer calls event.set() after 5 seconds


if __name__ == "__main__":
    main()
OP asks how to delete thread here on the python forum
(Apr-28-2025, 08:08 AM)buran Wrote: [ -> ]OP asks how to delete thread here on the python forum

So you can't delete threads at python-forum.io and threading.Thread doesn't allow you to delete/terminate threads either.

I've been here for a while and realized that I've never needed this function.