我正在使用python 2.7并且Python主线程在主程序退出后不会终止它的进程。python守护进程线程退出但进程仍在后台运行
我有以下线程类(Ubuntu的机器上-ax命令PS检查这一点),
import os
import threading
class captureLogs(threading.Thread):
'''
initialize the constructor
'''
def __init__(self, deviceIp, fileTag):
threading.Thread.__init__(self)
super(captureLogs, self).__init__()
self._stop = threading.Event()
self.deviceIp = deviceIp
self.fileTag = fileTag
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
'''
define the run method
'''
def run(self):
'''
Make the thread capture logs
'''
cmdTorun = "adb logcat > " + self.deviceIp +'_'+self.fileTag+'.log'
os.system(cmdTorun)
而且我创造了另一个文件sample.py一个线程,
import logCapture
import os
import time
c = logCapture.captureLogs('100.21.143.168','somefile')
c.setDaemon(True)
c.start()
print "Started the log capture. now sleeping. is this a dameon?", c.isDaemon()
time.sleep(5)
print "Sleep tiime is over"
c.stop()
print "Calling stop was successful:", c.stopped()
print "Thread is now completed and main program exiting"
我从命令行得到以下输出:
Started the log capture. now sleeping. is this a dameon? True
Sleep tiime is over
Calling stop was successful: True
Thread is now completed and main program exiting
然后sample.py退出。 但是,当我在终端上使用下面的命令,
ps -ax | grep "adb"
我仍然看到进程在运行。 (我正在使用kill -9 17681 17682手动杀死它们)
不知道我在这里丢失了什么。
我的问题是, 1)为什么当我已经在程序中杀死它时,进程仍然还活着?
2)如果我不打扰它会产生什么问题吗?
3)有没有其他更好的方法使用线程捕获日志并监视日志?
编辑:由于通过@bug杀手建议的,我添加了以下方法在我的线程类,
def getProcessID(self):
return os.getpid()
和我的示例中使用os.kill(c.getProcessID(),SIGTERM)。 py。该程序根本不退出。
通常,如果您守护进程,即使在parrent进程退出后,它仍会继续在后台运行 –
您是否已将SIGTERM发送到进程 –
我试过了。它似乎不适合我。 :( –