KeyboardInterrupt multiple threads at once

2020-06-28 09:58发布

问题:

I am currently working with several threads to collect data and save it in a JSON. The loop to collect the data is infinite. I want to be able to end all the threads with CTRL+C. Therefore I created this simple version with two loops. I have tried different things, but can't make it work so far. How can I use "except KeyboardInterrupt" to stop both loops at once? Or is there a better option?

import threading
from time import sleep

number = 0 
numberino = 10

def background():
    while True:
        if number < 10:
            global number
            number=number+1
            print number
            sleep(1)
        else:
            print "10 seconds are over!"
            break

def foreground():
    while True:
        if numberino > -10:
            global numberino
            numberino=numberino-1
            print numberino
            sleep(1)
        else:
            print "20 seconds are over!"
            break


b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()

回答1:

The simple way to do this is to have your threads check a global flag to see if it's time to exit. The general principle is that you shouldn't try to kill threads, you should ask them to exit, so they can close any resources they may have open.

I've modified your code so that the threads (including the original thread) check the global alive flag. BTW, you shouldn't put a global directive inside a loop, and it should be before any reference to the global variable(s) you want to modify. The best place is to put it at the top of the function.

import threading
from time import sleep

number = 0 
numberino = 10
alive = True

def background():
    global number
    while alive:
        if number < 10:
            number += 1
            print number
            sleep(1)
        else:
            print "10 seconds are over!"
            break

def foreground():
    global numberino
    while alive:
        if numberino > -10:
            numberino -= 1
            print numberino
            sleep(1)
        else:
            print "20 seconds are over!"
            break

b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()

while alive:
    try:
        sleep(.1)
    except KeyboardInterrupt:
        alive = False

print 'Bye'