簡體   English   中英

Python線程/子進程; 子進程仍在運行時,線程對象無效

[英]Python threading/subprocess; thread object invalid when subprocess is still running

我正在編寫一些監視工具,其中一個這樣的工具處理一系列NFS掛載的文件系統,並嘗試將測試文件寫入每個共享。 我為每個要測試的文件管理器生成一個線程。 超時后,我想終止所有仍在運行的線程。 我不需要從線程中收集輸出,不需要加入或連接到任何線程,我只需要停止超時后仍在運行的任何線程。

因此,我有意在每個線程中運行sleep(300),所以我不知道在我遍歷所有活動線程並嘗試殺死它們時,每個線程中生成的進程仍在運行。 這適用於5-20個線程中的任何一個,但最終在線程上失敗,提示self.pid不再有效。 發生這種情況的線程是隨機的,不一定與上次運行的線程相同。

class NFSWriteTestThread(threading.Thread):

    def __init__(self, filer, base_mnt_point, number):
        super(NFSWriteTestThread, self).__init__()
        self.tname    = filer
        self.tnum     = number
        self.filer    = filer
        self.mntpt    = base_mnt_point
        self.process  = None
        self.pid      = None

    def run(self):
        start = time.time()
#        self.process = subprocess.Popen(['/bin/dd', 'if=/dev/zero', 'bs=1M', 'count=5', 'of=' + self.testfile], shell=False)
        self.process = subprocess.Popen(['/bin/sleep', '300'], shell=False)
        time.sleep(1)
        logger.debug("DEBUG: %s=%d" % (self.tname, self.process.pid))
        self.pid     = self.process.pid
        logger.info("  NFS write test command initiaited on '%s', pid=%d" % (self.filer, self.pid))
        self.process.wait()
#        self.output, self.error = self.process.communicate()
        end = time.time()
        logger.info("  NFS write test for '%s' completed in %d seconds" % (self.filer, end - start))
        return

    def getThreadName(self):
        return self.tname

    def getThreadNum(self):
        return self.tnum

    def getThreadPID(self):
        if self.pid:
            return self.pid
        else:
            return "unknown"

    def isAlive(self):
        if not self.process:
            logger.debug("Error: self.process is invalid (%s)" % type(self.process))
#        if self.process.poll():
#            logger.info("NFS write test operation for thread '%s' is still active" % self.filer)
#        else:
#            logger.info("NFS write test operation for thread '%s' is inactive" % self.filer)
        return

    def terminate(self):
        os.kill(self.process.pid, signal.SIGTERM)
        return

    def kill(self):
        os.kill(self.process.pid, signal.SIGKILL)
        return

def initLogging(config):

    logfile   = os.path.join(config['logdir'], config['logfilename'])
    fformat   = logging.Formatter('%(asctime)s   %(message)s', "%Y-%m-%d %H:%M:%S %Z")
    cformat   = logging.Formatter('%(asctime)s   %(message)s', "%Y-%m-%d %H:%M:%S %Z")
    clogger   = None
    flogger   = None

    if config['debug']:
        loglevel = logging.DEBUG

    if not os.path.exists(config['logdir']):
        os.makedirs(config['logdir'])
        os.chmod(config['logdir'], 0700)
        os.chown(config['logdir'], 0, 0)

    try:
        logger = logging.getLogger('main')
        logger.setLevel(logging.DEBUG)

        # Define a file logger
        flogger = logging.FileHandler(logfile, 'w')
        flogger.setLevel(logging.DEBUG)
        flogger.setFormatter(fformat)
        logger.addHandler(flogger)

        # Define a console logger if verbose
        if config['verbose']:
            clogger = logging.StreamHandler()
            clogger.setLevel(logging.DEBUG)
            clogger.setFormatter(cformat)
            logger.addHandler(clogger)
    except Exception, error:
        print "Error: Unable to initialize file logging:  %s" % error
        sys.exit(1)

    logger.info("Script initiated.")

    logger.info("Using the following configuration:")
    for key, value in sorted(config.iteritems()):
        logger.info("    %20s = '%-s'" % (key, value))

    return logger

def parseConfigFile(cfg):

    if not os.path.isfile(cfg['cfgfile']) or not os.access(cfg['cfgfile'], os.R_OK):
        print "Error: '%s' does not exist or is not readable, terminating." % cfg['cfgfile']
        sys.exit(1)

    config = SafeConfigParser()
    config.read(cfg['cfgfile'])

    _cfg = dict(config.items(cfg['cfgfilestanza']))

    _cfgfilers = config.get(cfg['cfgfilestanza'], 'managed_filers')
    _tmpfilers = _cfgfilers.split(',')

    # populate a list containing all filers which will be meged into the global cfg[] dict
    _cfg['filers'] = []

    for _f in _tmpfilers:
        _cfg['filers'].append(_f.strip())

    return _cfg


logger = initLogging(cfg)
cfg.update(parseConfigFile(cfg))

threads     = []
numThreads  = 0

for filer in cfg['filers']:
    numThreads += 1
    logger.debug("  spawning NFS wite test thread for '%s', thread number %s" % (filer, numThreads))
    t = NFSWriteTestThread(filer, cfg['base_mnt_point'], numThreads)
    t.start()
    threads.append(t)
#    time.sleep(1)

logger.info("spawned %d NFS write test child threads" % numThreads)

logger.info("sleeping for %d seconds" % cfg['timeout'])
time.sleep(cfg['timeout'])

if (threading.activeCount() > 1):
    logger.info("there are %d NFS write test threads active after the timeout:" % (threading.activeCount() - 1))
    for thr in threading.enumerate():
        logger.debug("theadname=%s" % thr.name)
        if re.match("MainThread", thr.getName()):
            pass
        else:
            logger.info("thread '%s' (thread %d) is still alive" % (thr.getThreadName(), thr.getThreadNum()))
#            thr.isAlive()
            logger.info("killing thread for '%s' (pid=XX) with SIGTERM" % (thr.getThreadName()))
#            logger.info("killing thread for '%s' (pid=%d) with SIGTERM" % (thr.getThreadName(), thr.getThreadPID()))
            thr.kill()


logger.info("Script complete")
sys.exit(0)

在這里,您可以看到輸出:

2014-11-10 09:00:22 CST   there are 173 NFS write test threads active after the timeout:
2014-11-10 09:00:22 CST   theadname=Thread-165
2014-11-10 09:00:22 CST   thread 'hostname1' (thread 165) is still alive
2014-11-10 09:00:22 CST   killing thread for 'hostname1' (pid=XX) with SIGTERM
2014-11-10 09:00:22 CST   theadname=Thread-97
2014-11-10 09:00:22 CST   thread 'hostname2' (thread 97) is still alive
2014-11-10 09:00:22 CST     NFS write test for 'hostname1' completed in 60 seconds
2014-11-10 09:00:22 CST   killing thread for 'hostname2' (pid=XX) with SIGTERM
2014-11-10 09:00:22 CST   theadname=Thread-66
2014-11-10 09:00:22 CST   thread 'hostname3' (thread 66) is still alive
2014-11-10 09:00:22 CST     NFS write test for 'hostname2' completed in 60 seconds
2014-11-10 09:00:22 CST   killing thread for 'hostname3' (pid=XX) with SIGTERM
2014-11-10 09:00:22 CST   theadname=Thread-121
2014-11-10 09:00:22 CST   thread 'hostname4' (thread 121) is still alive
2014-11-10 09:00:22 CST   killing thread for 'hostname4' (pid=XX) with SIGTERM
Traceback (most recent call last):
2014-11-10 09:00:22 CST     NFS write test for 'hostname3' completed in 60 seconds
  File "./NFSWriteTestCheck.py", line 199, in <module>
    thr.kill()
  File "./NFSWriteTestCheck.py", line 84, in kill
    os.kill(self.process.pid, signal.SIGKILL)
AttributeError: 'NoneType' object has no attribute

在顯示此錯誤時,進程仍在運行,並在外殼中使用ps進行了驗證。 為什么線程對象不再有效? 在拋出此錯誤時,線程執行應該在這一點上:

self.process.wait()

只是在這里撓頭,想知道我是否遇到錯誤或其他問題。

您無法停止線程,只能中斷線程正在執行的操作。 在您的情況下,線程正在等待subprocess.call 設置事件無效,因為您的線程不會等待該事件。 解決方案是殺死子進程,這意味着您將需要Popen對象。

我將實現直接放入run方法中,以便Popen對象很方便。

class NFSWriteTestThread(threading.Thread):

    def __init__(self, filer, mntpoint, number):
        super(NFSWriteTestThread, self).__init__()
        self.name   = filer
        self.filer  = filer
        self.mntpt  = mntpoint
        self.tnum   = number
        self._proc  = None

    def run(self):
        testfile = "%s/%s/test/test.%s" % (mountpoint, filer, filer)
        testcmd  = "/bin/bash -c '/bin/dd if=/dev/zero bs=1024 count=1024 of=" + testfile + " >/dev/null 2>/dev/null; sleep 120'"
        self._proc = subprocess.Popen(testcmd, shell=True)
        self._proc.wait()
        return

    def getName(self):
        return self.name

    def kill(self):
        if self._proc:
            self._proc.terminate()

    def stopped(self):
        if self._proc:
            return self._proc.poll() is not None
        else:
            return True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM