S
S
Sanchoys2282020-08-14 15:40:26
Python
Sanchoys228, 2020-08-14 15:40:26

How to stop all daemon threads on button click?

There is a STOP button in the interface, it is necessary that after pressing it, all demonic threads are completed

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2020-08-14
@LaRN

You can use the threading library, there is such an entity as event. Using event, you can send a signal to all threads, and threads receiving this signal can either suspend or self-destruct.
An example of usage is here:
https://www.bogotobogo.com/python/Multithread/pyth...

T
Ternick, 2020-08-14
@Ternick

1) The easiest way to do this is using the multiprocessing library.
2) Something like this:

code

import multiprocessing 
import time 

def func(number): 
  for i in range(1, 10): 
    time.sleep(0.01) 
    print('Processing ' + str(number) + ': prints ' + str(number*i)) 

# list of all processes, so that they can be killed afterwards 
all_processes = [] 

for i in range(0, 3): 
  process = multiprocessing.Process(target=func, args=(i,)) 
  process.start() 
  all_processes.append(process) 

# kill all processes after 0.03s 
time.sleep(0.03) 
for process in all_processes: 
  process.terminate()


If you are happy with the answer, mark the answer as the answer :)
Good luck :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question