![](/img/trans.png)
[英]Using subprocess.Popen to start a program in Windows - subprocess sometimes gets stuck - is my script stable?
[英]subprocess.Popen gets stuck for long time?
當我在外殼上運行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)
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
有幾個問題,例如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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.