我有一个运行启动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
。为什么流程会消失?