简体   繁体   English

subprocess.Popen卡了很长时间吗?

[英]subprocess.Popen gets stuck for long time?

When i run the python script ( BootScript.py ) on the shell it runs properly but when i try to run it through another script( automation.py ) it gets stuck 当我在外壳上运行python脚本(BootScript.py)时,它会正常运行,但是当我尝试通过另一个脚本(automation.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)
I am trying to automate BootScript.py 
**What the Program Does ?**
Here it runs BootScript.py which arguments . the output of the bootscript.py is checked for specific line (BOOTSCRIPT SCORE IS: 3010)
If present it is asumed as to sucess else failure , this script is run for counter number of times

**What i want?**
This script gets stuck for a long time , i want it to execute with out beeing sstuck , as though i am running the bootscript manually

There are several issues eg, Popen(['python' ,'bdxBootScript.py', mylist ]) should raise an exception because you should use Popen(['python' ,'bdxBootScript.py'] + mylist) instead. 有几个问题,例如Popen(['python' ,'bdxBootScript.py', mylist ])应该引发异常,因为您应该改用Popen(['python' ,'bdxBootScript.py'] + mylist) If you don't see the exception then either the code is not run eg, counter==0 or (worse) you suppress exceptions up the stack (don't do it, at the very least you should log unexpected errors). 如果没有看到异常,则代码都不会运行,例如counter==0或(更糟糕),您会在堆栈中抑制异常(不要这样做,至少您应该记录意外错误)。

If bdxBootScript.py doesn't produce much output then for line in proc.stdout: may appear to do nothing for some time, to fix it pass -u flag to python to make its output unbuffered and use iter(p.stdout.readline, b'') to workaround the "hidden read-ahead buffer" bug for pipes in Python 2: 如果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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM