Python,几个线程中的子进程都消失了

时间:2016-10-07 19:24:10

标签: python multithreading subprocess

我有一个运行启动subprocess.Popen的线程的脚本。该脚本监视子进程的所有输出,并使操作依赖于sobprocess'es输出。但有时脚本会关闭线程。以下是我的脚本示例:

import threading
import subprocess
import os

def doAction(): # Main actions which every thread do.
    command = "MyCommandForSubprocess"
    p = subprocess.Popen(command, 
                         stdout=subprocess.PIPE, 
                         stderr=subprocess.STDOUT, 
                         shell=True, 
                         preexec_fn=os.setsid)
    command_output = iter(p.stdout.readline, b'')

    while True: # Linstening of subprocess output.
        for line in command_output:
            if "(i)" in line:
                OutputMessages("Good News")
                parameter += 1
            if "(done)" in line:
                os.killpg(os.getpgid(p.pid), signal.SIGTERM)
        break

    processesTested += 1
    OutputMessages("done")

def main()
    threadsLimited = raw_input("How many threads? ")
    threadsLimited = int(threadsLimited)

    actions = ["Thread(target=doAction, args=(parameter,))", "..."]

    for action in actions:
        # Limiter to chose how many threads to start simuntaneously.
        while True:
            if(int(activeCount()) <= threadsLimited):
                action.start()
                break
            else:
                time.sleep(.1)
                continue

    for thread in checkThreads:
        thread.join()
        activeCount()

当它启动所有进程时,我可以在系统监视器中找到所有python进程。但几分钟后,python进程已关闭,但sh进程仍处于活动状态且内存使用量为n/a。为什么流程会消失?

0 个答案:

没有答案