【发布时间】:2018-11-19 12:45:10
【问题描述】:
我有一个类,其中包含一些基本上对数据进行输出检查的函数,这个类的函数是使用子进程调用的。 现在,如果输出检查失败,子进程会有一个 sys.exit 调用,其中包含不同的代码,具体取决于它失败的检查。
在主代码中我有这个:
try:
exitCode = 0
#import module for current test
teststr = os.path.splitext(test)[0]
os.chdir(fs.scriptFolder)
test = __import__(teststr)
#delete old output folder and create a new one
if os.path.isdir(fs.outputFolder):
rmtree(fs.outputFolder)
os.mkdir(fs.outputFolder)
# run the test passed into the function as a new subprocess
os.chdir(fs.pythonFolder)
myEnv=os.environ.copy()
myEnv["x"] = "ON"
testSubprocess = Popen(['python', test.testInfo.network + '.py', teststr], env=myEnv)
testSubprocess.wait()
result = addFields(test)
# poke the data into the postgresql database if the network ran successfully
if testSubprocess.returncode == 0:
uploadToPerfDatabase(result)
elif testSubprocess.returncode == 1:
raise Exception("Incorrect total number of rows on output, expected: " + str(test.testInfo.outputValidationProps['TotalRowCount']))
exitCode = 1
elif testSubprocess.returncode == 2:
raise Exception("Incorrect number of columns on output, expected: " + str(test.testInfo.outputValidationProps['ColumnCount']))
exitCode = 1
except Exception as e:
log.out(teststr + " failed", True)
log.out(str(e))
log.out(traceback.format_exc())
exitCode = 1
return exitCode
现在,此输出显示子进程中 sys.exit 调用的所有回溯和 python 异常。 我实际上记录了所有错误,所以我不希望在命令提示符中显示任何内容,除非我手动打印它。 我不太确定该怎么做。
【问题讨论】:
标签: python python-3.x exception subprocess popen