python使用Ctrl+C中断threading多线程死循环及setDaemon守护进程

来源:互联网 发布:淘宝助理价格设置 编辑:程序博客网 时间:2024/05/20 12:51

threading多线程,没有提供线程退出的方法。
如果在死循环多线程,则即使CTRL+C也没有任何效果:
例如:

#coding=utf8import threadingimport timedef showperson(name):    while True:       time.sleep(1)       print 'show person :%s'%nameprint '%s thread start!'%(time.ctime())list=[]for i in range(3):  t =threading.Thread(target=showperson,args=("person-%d"%i,))  list.append(t)  t.start()for threadinglist in list:    threadinglist.join()print '%s thread end!'%(time.ctime())

输出:

[root@myrpc zrd]# python threadkill.py Fri May 26 22:38:37 2017 thread start!show person :person-0show person :person-1show person :person-2^C^Cshow person :person-0show person :person-1show person :person-2^C^C^C^Cshow person :person-0show person :person-1show person :person-2^C^C^C^Cshow person :person-0show person :person-1show person :person-2^C^C^C^C^Cshow person :person-1show person :person-0show person :person-2^C^C^C^C^Cshow person :person-1show person :person-0show person :person-2show person :person-1

最后只有kill进程才能退出:

# ps -ef | grep pythonroot       1781   1635  0 May25 pts/1    00:00:00 pythonroot       1824   1635  0 May25 pts/1    00:00:00 pythonroot       5479   5451  0 22:04 pts/2    00:00:00 pythonroot       5576   5550  0 22:38 pts/3    00:00:00 python threadkill.pyroot       5600   5580  0 22:38 pts/4    00:00:00 grep pythonkill -9 1781 1824 5479 5576

那怎么样才能退出呢,可以设置守护进程setDaemon
并且不能使用join将进程挂起,否则也无法退出,但是并不能使用join,则会重复上面的问题,
这里使用while空循环
修改如下:

#coding=utf8import threadingimport timedef showperson(name):    while True:       time.sleep(1)       print 'show person :%s'%nameprint '%s thread start!'%(time.ctime())list=[]for i in range(3):  t =threading.Thread(target=showperson,args=("person-%d"%i,))  list.append(t)  t.setDaemon(True)  t.start()while True:    passprint '%s thread end!'%(time.ctime())

输出:

# python threadkill.py Fri May 26 22:47:17 2017 thread start!show person :person-0show person :person-1show person :person-2^CTraceback (most recent call last):  File "threadkill.py", line 19, in <module>    while True:KeyboardInterrupt

使用CTRL+C退出了程序,但是抛出一个异常
解决方法
1.设置异常处理,抛出一个提示程序退出

#coding=utf8import threadingimport timedef showperson(name):    while True:       time.sleep(1)       print 'show person :%s'%nameprint '%s thread start!'%(time.ctime())try:   list=[]   for i in range(3):       t =threading.Thread(target=showperson,args=("person-%d"%i,))       list.append(t)       t.setDaemon(True)       t.start()   while True:       passexcept KeyboardInterrupt,e:    print "you stop the threading"print '%s thread end!'%(time.ctime())

输出:

# python threadkill.py Fri May 26 23:01:00 2017 thread start!show person :person-0show person :person-1show person :person-2^Xshow person :person-0show person :person-1show person :person-2^X^X^X^Xshow person :person-0show person :person-1show person :person-2^A^A^Ashow person :person-0show person :person-1show person :person-2^Cyou stop the threadingFri May 26 23:01:15 2017 thread end!

CTRL+c的时候才退出

2.编写quit退出,使用signal信号传递

#coding=utf8import threadingimport timeimport sysimport signaldef showperson(name):    while True:       time.sleep(1)       print 'show person :%s'%nameprint '%s thread start!'%(time.ctime())def quit(signal_num,frame):    print "you stop the threading"    sys.exit()try:   signal.signal(signal.SIGINT, quit)   signal.signal(signal.SIGTERM, quit)   list=[]   for i in range(3):       t =threading.Thread(target=showperson,args=("person-%d"%i,))       list.append(t)       t.setDaemon(True)       t.start()   while True:       passexcept Exception,e:    print eprint '%s thread end!'%(time.ctime())

结果:

Fri May 26 23:10:43 2017 thread start!show person :person-0show person :person-1show person :person-2show person :person-0show person :person-1show person :person-2^Cyou stop the threading

但是这个退出,不会进行后面的步骤,而之前的异常捕获处理后会进行后续步骤

阅读全文
0 0
原创粉丝点击