import sysimport signalimport timeimport threading
def signalHandler(sig,id):
sys.exit()signal.signal(signal.SIGINT, signalHandler)signal.signal(signal.SIGQUIT, signalHandler)signal.signal(signal.SIGTERM, signalHandler)signal.signal(signal.SIGHUP, signalHandler)
class Test(threading.Thread):
def run(self): while True:
print 'xxxxxxxxxxx'
time.sleep(1)
def main():
t = Test()
t.start()
if __name__ == '__main__':
main()import sysimport signalimport timeimport threading
NEEDEXIT = False
def signalHandler(sig,id):
global NEEDEXIT
NEEDEXIT = True
sys.exit()signal.signal(signal.SIGINT, signalHandler)signal.signal(signal.SIGQUIT, signalHandler)signal.signal(signal.SIGTERM, signalHandler)signal.signal(signal.SIGHUP, signalHandler)
class Test(threading.Thread):
def run(self):
global NEEDEXIT while not NEEDEXIT:
print 'xxxxxxxxxxx'
time.sleep(1)
def main():
t = Test()
t.start()
if __name__ == '__main__':
main()
1 回答
米脂
TA贡献1836条经验 获得超3个赞
我所知道的python是没有像ruby一类的thread kill的,所以一般都采用一个全局变量,然后在thread内部去check这个全局变量,如果全局变量changed了,那么就退出这个thread,所以意味着你可以在外部去修改这个全局变量。
添加回答
举报
0/150
提交
取消