如何暂停 python 脚本以进一步执行,但子进程必须运行指定的时间


p = subprocess.Popen("exec " +runcmd, stdout=subprocess.PIPE, shell=True)
            while p.poll() == None:
            #while True:
                    output = p.stdout.readline()
                    print (output)
                    if output == '' and p.poll is not None:
                            break
                    if output:
                            str = output.strip()
                            if '"Activate"' in str:
                                    print ("pause initiated")

在这里,当 stdout/ouput 在日志中找到激活时,我想暂停脚本,但子进程执行必须在指定的时间内继续??如果我在某处错了,请帮助并纠正我

我仍然不能 100% 确定您要查找的内容,但以下脚本:

  1. 启动 runcmd(在命令之前不需要exec(
  2. 等到它在输出中找到"Activate"(或者在程序之前结束时停止,这在.poll()调用中已经正确(
  3. 睡10秒
  4. 终止进程并中断循环

 

import subprocess
import time
p = subprocess.Popen(runcmd, stdout=subprocess.PIPE, shell=True)
    while p.poll() == None:
        output = p.stdout.readline()
        print (output)
        if output == '' and p.poll is not None:
            break
        if output:
            s = output.strip()
            if '"Activate"' in s:
                print ("pause initiated")
                time.sleep(10) # wait 10 seconds
                return_value = p.kill()
                break # explicit break, this is not needed as p.poll() would return -9

最新更新