Discussion:
How to stop a specific thread in Python 2.7?
(too old to reply)
marc nicole
2024-09-25 17:24:49 UTC
Permalink
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!
Stefan Ram
2024-09-25 17:39:37 UTC
Permalink
Post by marc nicole
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.
Lawrence D'Oliveiro
2024-09-25 21:42:10 UTC
Permalink
Post by Stefan Ram
Killing or stopping a thread can cause data corruption and
unpredictable behavior.
Interesting that even the underlying POSIX thread-terminating call
<https://manpages.debian.org/3/pthread_cancel.3.en.html> allows for the
concept of “cancellation points”, so that threads can elect to only be
terminated at well-defined points in their execution.
Cameron Simpson
2024-09-25 20:44:09 UTC
Permalink
Post by marc nicole
I want to know how to kill a specific running thread (say by its id)
# 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 <***@cskk.id.au>
marc nicole
2024-09-25 20:56:58 UTC
Permalink
How to create a per-thread event in Python 2.7?

On Wed, 25 Sept 2024, 22:47 Cameron Simpson via Python-list, <
Post by Cameron Simpson
Post by marc nicole
I want to know how to kill a specific running thread (say by its id)
# 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,
--
https://mail.python.org/mailman/listinfo/python-list
Cameron Simpson
2024-09-26 01:06:03 UTC
Permalink
Post by marc nicole
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.
marc nicole
2024-09-26 01:34:05 UTC
Permalink
Could you show a python code example of this?
Post by Cameron Simpson
Post by marc nicole
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.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
Left Right
2024-09-25 20:14:43 UTC
Permalink
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
Post by marc nicole
Hello guys,
I want to know how to kill a specific running thread (say by its id)
# 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
Dan Ciprus (dciprus)
2024-10-03 22:12:15 UTC
Permalink
I'd be interested too :-).
Post by marc nicole
Could you show a python code example of this?
Post by Cameron Simpson
Post by marc nicole
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.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
https://mail.python.org/mailman/listinfo/python-list
--
Dan Ciprus

[ curl -L http://git.io/unix ]
Cameron Simpson
2024-10-03 23:17:19 UTC
Permalink
Post by Dan Ciprus (dciprus)
I'd be interested too :-).
Untested sketch:

def make_thread(target, *a, E=None, **kw):
'''
Make a new Event E and Thread T, pass `[E,*a]` as the target
positional arguments.
A shared preexisting Event may be supplied.
Return a 2-tuple of `(T,E)`.
'''
if E is None:
E = Event()
T = Thread(target=target, args=[E, *a], kwargs=kw)
return T, E

Something along those lines.

Cheers,
Cameron Simpson <***@cskk.id.au>
Dan Ciprus (dciprus)
2024-10-11 18:32:40 UTC
Permalink
Thank you for the hint !
Post by Cameron Simpson
Post by Dan Ciprus (dciprus)
I'd be interested too :-).
'''
Make a new Event E and Thread T, pass `[E,*a]` as the target
positional arguments.
A shared preexisting Event may be supplied.
Return a 2-tuple of `(T,E)`.
'''
E = Event()
T = Thread(target=target, args=[E, *a], kwargs=kw)
return T, E
Something along those lines.
Cheers,
--
Dan Ciprus

[ curl -L http://git.io/unix ]
Loading...