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

2024-09-25 Thread marc nicole via Python-list
Could you show a python code example of this?


On Thu, 26 Sept 2024, 03:08 Cameron Simpson,  wrote:

> On 25Sep2024 22:56, marc nicole  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.
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2024-09-25 Thread Cameron Simpson via Python-list

On 25Sep2024 19:24, marc nicole  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 
--
https://mail.python.org/mailman/listinfo/python-list


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

2024-09-25 Thread marc nicole via Python-list
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  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 
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2024-09-25 Thread Cameron Simpson via Python-list

On 25Sep2024 22:56, marc nicole  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.

--
https://mail.python.org/mailman/listinfo/python-list


How to stop a specific thread in Python 2.7?

2024-09-25 Thread marc nicole via Python-list
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