我在测试多线程时,发现有写模块无法在多线程下正确响应 ctrl + c,经过我测试,应该为 paste 模块所致,请问这种情况如何处理较好?
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)
我现有的解决方案为采用 multiprocessing 库来解决程序退出问题。
threading.Condition
python3