当我在外壳上运行python脚本(BootScript.py)时,它会正常运行,但是当我尝试通过另一个脚本(automation.py)运行它时,它会卡住

//automation.py

#!/usr/bin/env python
import sys
import optparse
import subprocess
global flag



failcount = 0

def incrfailcount():
    global failcount
    failcount += 1

def readfile():
    fp = open('BootStrap.log','r')
    print "press any key"
    #_input()
    for l in fp.readlines() :


        if "BOOTSCRIPT SCORE IS: 3010" in l :
            #import pdb
            #pdb.set_trace()
            global flag
            flag = 0
    fp.close()

parser = optparse.OptionParser()
parser.add_option('-c', '--count', dest='counter', help='no of time reboot Should Happen')


(options, args) = parser.parse_args()
#counter = 1
if options.counter is None:
    counter = 1
else :
    counter = options.counter
count = 0
output = ""
mylist = [ ' --cfgfile="BDXT0_PO_0.cfg"' , ' --cfgfile="BDXT0_PO_OVR_0.cfg"' ,' --scbypass' , ' --dmipy="C:\\sfd\\jg\\kdg\\dmi_pcie_po.py"', '  --fusestr="IA_CORE_DISABLE=0y111111111111111111111110"' , ' --fusestr="HT_DIS=1"' , ' --earbreakpy="C:\\dvfdfv\\dskf\\lsvcd\\config_restart.py"']

logfile = open('BootStrap.log', 'w')


    #if out.__contains__('3010') :
        #break
for i in range(int(counter)):
    global flag
    flag = 1
    logfile = open('BootStrap.log', 'w')
    proc = subprocess.Popen(['python' ,'bdxBootScript.py', mylist ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in proc.stdout:
        sys.stdout.write(line)
        logfile.write(line)
    proc.wait()

    count = count + 1
    print "file closing "
    logfile.close()
    readfile()
    #global flag
    if flag :
        incrfailcount()
        continue





if flag :
    print "Error Occured in %d th iteration" %count
else :
    print "Every thing Went well"






if failcount >= 0 :
    print "Script failed %d times of total run %d " %(failcount, count)


我正在尝试自动化BootScript.py
**程序做什么?**
在这里,它运行带有参数的BootScript.py。在bootscript.py的输出中检查特定行(BOOTSCRIPT SCORE IS:3010)
如果存在,则假定成功,否则此脚本将运行计数器次数

**我想要的是?**
这个脚本被卡住了很长时间,我希望它在执行过程中不会被蜜蜂st住,就像我手动运行启动脚本一样

最佳答案

有几个问题,例如Popen(['python' ,'bdxBootScript.py', mylist ])应该引发异常,因为您应该改用Popen(['python' ,'bdxBootScript.py'] + mylist)。如果您没有看到异常,则代码不会运行,例如counter==0或(更糟糕),您可以在堆栈中抑制异常(不要这样做,至少您应该记录意外错误)。

如果bdxBootScript.py不会产生太多输出,则for line in proc.stdout:可能在一段时间内似乎无任何作用,要对其进行修复,请将-u标志传递给python以使其输出不被缓冲,并使用iter(p.stdout.readline, b'')来解决“隐藏”问题。 Python 2中管道的“预读缓冲区”错误:

import os
import sys
from subprocess import Popen, PIPE, STDOUT

with open(os.devnull, 'rb', 0) as DEVNULL:
    proc = Popen([sys.executable, '-u', 'bdxBootScript.py'] + mylist,
                 stdin=DEVNULL, stdout=PIPE, stderr=STDOUT, bufsize=1)
for line in iter(proc.stdout.readline, b''):
    sys.stdout.write(line)
    sys.stdout.flush()
    logfile.write(line)
    logfile.flush() # make the line available in the log immediately
proc.stdout.close()
rc = proc.wait()

关于python - subprocess.Popen长时间卡住了吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23215859/

10-09 03:38