我想使用子進程調用另一個本地腳本。調用另一個python腳本的main()並獲取STDOUT結果
本地腳本將其結果打印到屏幕上。
地方:
def main():
"""
main function
controls the method call, the method is decided by TS runner
"""
sParams = ScriptParamHandler()
paramsList = ['TestName'] # ordered list of parameters
paramsDict = sParams.AnalyzeParametersForScript(paramsList)
mainReturn = None
with ProductXXXXX() as testSequence:
testSequence.testFixture.InitDevices()
func = getattr(testSequence, paramsDict['TestName'])
returnVal = func()
print paramsDict['TestName'], "\n", returnVal, "\n"
if __name__ == "__main__":
main()
來電腳本:
with BasicRunner(testSet, testSetLocation, paramsDict['ProfileName']) as testRunner:
if testRunner.CheckFolderAndFile():
testRunner.GetProfile()
for test in testRunner.testList:
testRunner.logger.Info("Test {} started...".format(test))
testResult = subprocess.call(testRunner.tsPyFile + " " + test, shell=True)
testRunner.logger.Info("Test {} result {}".format(test, testResult))
else:
pass
我想主叫腳本testResult
是當地腳本stout
。
我試過stdout=subprocess.PIPE
和subprocess.check_output()
但沒有運氣,也許有人能給我一些更好的方向?
你嘗試testResult.communicate(),而不是subprocess.check_output()? (使用stdout = subprocess.PIPE of course) – shshank
當腳本在同一個python進程中運行時,您也可以重定向stdout。雖然[如果子腳本生成非Python輸出(比如來自外部子進程,C擴展庫等的輸出)] [更復雜](https://gist.github.com/zed/991e006c822740aa6896#file-parent-py)。 – jfs