How to correctly respond to ctrl + c under python multithreading

when I was testing multithreading, I found that a write module could not correctly respond to ctrl + c under multithreading. After my test, it should be caused by the paste module. How to deal with this situation?

import sys
import threading
import time

import bottle

HttpThread1 = None
HttpThread2 = None


@bottle.route("/hello")
def hello():
    return "Hello World!"


def server1():
    bottle.run(server="paste", port=8081)


def server2():
    bottle.run(server="paste", port=8082)


def info():
    print(threading.active_count())


try:
    HttpThread1 = threading.Thread(target=server1, args=())
    HttpThread1.setDaemon(True)
    HttpThread1.start()

    HttpThread2 = threading.Thread(target=server2, args=())
    HttpThread2.setDaemon(True)
    HttpThread2.start()

    while True:
        info()
        time.sleep(1)
except KeyboardInterrupt:
    sys.exit(1)
< hr >

my current solution is to use the multiprocessing library to solve the program exit problem.


threading.Condition

python3

  @ Yujiaao  this will not work, and there will be no problem with subthreading programs that can hibernate. 

however, bottle and paste do webserver, which means that what runs in run does not end, and bottle does not have an API that opens paste (at least the call is not open).

ctrl + c needs to pass the event through to the child thread, ending the child thread first, and then the main thread.

I haven't tested the signal processing yet. I'm going to test and turn off the signal processing to see if I can solve it.

Menu