python 中使用 signal 比较简单,理论上多线程下也可以保证接受到,处理掉各种平台上不一致的问题。
-----------------------------------------------------
import signal, threading
stop_thread = False
def handler(signum, frame):
print "sig"
global stop_thread
stop_thread = True
class MyThread(threading.Thread):
def run(self):
print "1111 start"
while not stop_thread:
pass
print "1111 done"
signal.signal(signal.SIGUSR1, handler)
thr = MyThread()
thr.start()
print "2222"
while not stop_thread:
pass
stop_thread = False
print "3333"
while not stop_thread:
pass
print "4444"
-----------------------------------------------------
评论