Python,子进程在少数线程中是不存在的

2024-04-26 12:11:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它运行从subprocess.Popen开始的线程。这个脚本监视子进程的所有输出,并使操作依赖于sobprocess的输出。但有时脚本会关闭线程。下面是我的脚本示例:

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。为什么会出现进程?你知道吗


Tags: inimport脚本actionstrueforoutputif