A
A
Andrey2018-05-17 19:42:16
Python
Andrey, 2018-05-17 19:42:16

How to stop a thread and the whole python3 script?

Made multithreading and now ctrl+c doesn't stop the script, only closing the window.
Example:

import threading

def scan(): 
    for line in f:
        try:  
            do_something  
        except SomeError:
            pass
thread_scan1 = threading.Thread(target=scan, name='s1')  
thread_scan2 = threading.Thread(target=scan, name='s2')  
thread_scan3 = threading.Thread(target=scan, name='s3')  
thread_scan4 = threading.Thread(target=scan, name='s4')  
thread_scan5 = threading.Thread(target=scan, name='s5')  

thread_scan1.start()  
thread_scan2.start()  
thread_scan3.start()  
thread_scan4.start()  
thread_scan5.start()  

thread_scan1.join()  
thread_scan2.join()  
thread_scan3.join()  
thread_scan4.join()  
thread_scan5.join()

How to complete it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spaceatmoon, 2018-05-17
@spaceatmoon

God help. There is an answer there.
https://www.linux.org.ru/forum/development/5632316
Also look here, but the first one should be enough to start.
https://stackoverflow.com/questions/18018033/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question