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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/comprel/article/details/72798331

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

#coding=utf8
import threading
import time

def showperson(name):
    while True:
       time.sleep(1)
       print 'show person :%s'%name

print '%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-0
show person :person-1
show person :person-2
^C^Cshow person :person-0
show person :person-1
show person :person-2
^C^C^C^Cshow person :person-0
show person :person-1
show person :person-2
^C^C^C^Cshow person :person-0
show person :person-1
show person :person-2
^C^C^C^C^Cshow person :person-1
show person :person-0
show person :person-2
^C^C^C^C^Cshow person :person-1
show person :person-0
show person :person-2
show person :person-1

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

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

kill -9 1781 1824 5479 5576

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

#coding=utf8
import threading
import time

def showperson(name):
    while True:
       time.sleep(1)
       print 'show person :%s'%name

print '%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:
    pass

print '%s thread end!'%(time.ctime())

输出:

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

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

#coding=utf8
import threading
import time

def showperson(name):
    while True:
       time.sleep(1)
       print 'show person :%s'%name

print '%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:
       pass
except 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-0
show person :person-1
show person :person-2
^Xshow person :person-0
show person :person-1
show person :person-2
^X^X^X^Xshow person :person-0
show person :person-1
show person :person-2
^A^A^Ashow person :person-0
show person :person-1
show person :person-2
^Cyou stop the threading
Fri May 26 23:01:15 2017 thread end!

CTRL+c的时候才退出

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

#coding=utf8
import threading
import time
import sys
import signal

def showperson(name):
    while True:
       time.sleep(1)
       print 'show person :%s'%name

print '%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:
       pass
except Exception,e:
    print e

print '%s thread end!'%(time.ctime())

结果:

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

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

猜你喜欢

转载自blog.csdn.net/comprel/article/details/72798331