简体   繁体   English

python守护程序线程退出,但进程仍在后台运行

[英]python daemon thread exits but process still run in the background

I am using python 2.7 and Python thread doesn't kill its process after the main program exits. 我正在使用python 2.7,并且在主程序退出后Python线程不会终止其进程。 (checking this with the ps -ax command on ubuntu machine) (在ubuntu机器上使用ps -ax命令进行检查)

I have the below thread class, 我有下面的线程类,

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)

And I am creating a thread in another file sample.py, 我正在另一个文件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"

I get the below output from the command line: 我从命令行获得以下输出:

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

And the sample.py exits. 然后sample.py退出。 But when I use below command on a terminal, 但是当我在终端上使用下面的命令时,

ps -ax | grep "adb"

ubuntu机器上ps命令的输出

I still see the process running. 我仍然看到进程正在运行。 (I am killing them manually now using the kill -9 17681 17682) (我现在使用kill -9 17681 17682手动杀死它们)

Not sure what I am missing here. 不知道我在这里想念的是什么。

My question is, 1) why is the process still alive when I already killed it in my program? 我的问题是:1)为什么我在程序中将其杀死后,该过程仍然存在?

2) Will it create any problem if I don't bother about it? 2)如果我不理会它会产生任何问题吗?

3) is there any other better way to capture logs using a thread and monitor the logs? 3)还有其他更好的方法来使用线程捕获日志并监视日志吗?

EDIT: As suggested by @bug Killer, I added the below method in my thread class, 编辑:正如@bug Killer所建议,我在线程类中添加了以下方法,

def getProcessID(self):
        return os.getpid()

and used os.kill(c.getProcessID(), SIGTERM) in my sample.py . 并在我的sample.py中使用了os.kill(c.getProcessID(),SIGTERM) The program doesn't exit at all. 该程序根本不会退出。

It is likely because you are using os.system in your thread. 可能是因为您在线程中使用os.system The spawned process from os.system will stay alive even after the thread is killed. 即使杀死线程,从os.system派生的进程os.system将保持活动状态。 Actually, it will stay alive forever unless you explicitly terminate it in your code or by hand (which it sounds like you are doing ultimately) or the spawned process exits on its own. 实际上,除非您在代码中或用手工方式明确终止它(这听起来像您最终正在做的事情)或所生成的进程自行退出,否则它将一直存在。 You can do this instead: 您可以改为:

import atexit
import subprocess

deviceIp = '100.21.143.168'
fileTag = 'somefile'

# this is spawned in the background, so no threading code is needed
cmdTorun = "adb logcat > " + deviceIp +'_'+fileTag+'.log'
proc = subprocess.Popen(cmdTorun, shell=True)

# or register proc.kill if you feel like living on the edge
atexit.register(proc.terminate)

# Here is where all the other awesome code goes

Since all you are doing is spawning a process, creating a thread to do it is overkill and only complicates your program logic. 由于您所要做的只是产生一个进程,因此创建一个线程来执行它是过大的,只会使您的程序逻辑复杂化。 Just spawn the process in the background as shown above and then let atexit terminate it when your program exits. 只需如上所述在后台生成该进程,然后在程序退出时让atexit终止它即可。 And/or call proc.terminate explicitly; 和/或显式调用proc.terminate ; it should be fine to call repeatedly (much like close on a file object) so having atexit call it again later shouldn't hurt anything. 重复调用(就像close文件对象一样)应该没问题,因此稍后再次使用atexit调用不会有任何伤害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM