Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python subprocess.call输出不是交错的_Python_Subprocess - Fatal编程技术网

python subprocess.call输出不是交错的

python subprocess.call输出不是交错的,python,subprocess,Python,Subprocess,我有一个运行其他shell脚本的python v3.3脚本。我的python脚本还打印消息,如即将运行脚本X和已完成运行脚本X 当我运行我的脚本时,我将获得与print语句分离的shell脚本的所有输出。我看到这样的情况: All of script X's output All of script Y's output All of script Z's output About to run script X Done running script X About to run script

我有一个运行其他shell脚本的python v3.3脚本。我的python脚本还打印消息,如即将运行脚本X和已完成运行脚本X

当我运行我的脚本时,我将获得与print语句分离的shell脚本的所有输出。我看到这样的情况:

All of script X's output
All of script Y's output
All of script Z's output
About to run script X
Done running script X
About to run script Y
Done running script Y
About to run script Z
Done running script Z
print( "running command: " + cmnd )
ret_code = subprocess.call( cmnd, shell=True )
print( "done running command")
运行shell脚本的代码如下所示:

All of script X's output
All of script Y's output
All of script Z's output
About to run script X
Done running script X
About to run script Y
Done running script Y
About to run script Z
Done running script Z
print( "running command: " + cmnd )
ret_code = subprocess.call( cmnd, shell=True )
print( "done running command")
我写了一个基本的测试脚本,没有看到这种行为。此代码符合我的预期:

print("calling")
ret_code = subprocess.call("/bin/ls -la", shell=True )
print("back")

知道为什么输出不是交错的吗?

这与标准输出和标准输出缓冲有关。您应该使用subprocess.Popen将STDOUT和STDERR从子进程重定向到应用程序中。然后,根据需要输出它们。例如:

import subprocess

cmd = ['ls', '-la']
print('running command: "{0}"'.format(cmd))  # output the command.
# Here, we join the STDERR of the application with the STDOUT of the application.
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.wait()  # Wait for the underlying process to complete.
out, err = process.communicate()  # Capture what it outputted on STDOUT and STDERR
errcode = process.returncode  # Harvest its returncode, if needed.
print(out)
print('done running command')

此外,除非确实需要,否则我不会使用shell=True。它强制子进程启动整个shell环境,只是为了运行命令。通常最好直接注入Popen的env参数。

这与STDOUT和STDERR缓冲有关。您应该使用subprocess.Popen将STDOUT和STDERR从子进程重定向到应用程序中。然后,根据需要输出它们。例如:

import subprocess

cmd = ['ls', '-la']
print('running command: "{0}"'.format(cmd))  # output the command.
# Here, we join the STDERR of the application with the STDOUT of the application.
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.wait()  # Wait for the underlying process to complete.
out, err = process.communicate()  # Capture what it outputted on STDOUT and STDERR
errcode = process.returncode  # Harvest its returncode, if needed.
print(out)
print('done running command')

此外,除非确实需要,否则我不会使用shell=True。它强制子进程启动整个shell环境,只是为了运行命令。通常最好直接注入Popen的env参数。

谢谢。这可以工作,但有一个限制-在命令完成之前,您无法看到任何输出。我从另一个问题中找到了答案,这个问题使用了popen,但也可以让我实时查看输出。以下是我最终得出的结论:

import subprocess
import sys

cmd = ['/media/sf_git/test-automation/src/SalesVision/mswm/shell_test.sh', '4', '2']
print('running command: "{0}"'.format(cmd))  # output the command.
# Here, we join the STDERR of the application with the STDOUT of the application.
process = subprocess.Popen(cmd, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
    line = line.replace('\n', '')
    print(line)
    sys.stdout.flush()
process.wait()                   #  Wait for the underlying process to complete.
errcode = process.returncode      #  Harvest its returncode, if needed.
print( 'Script ended with return code of: ' + str(errcode) )

这将使用Popen并允许我查看所调用脚本的进度。

谢谢。这可以工作,但有一个限制-在命令完成之前,您无法看到任何输出。我从另一个问题中找到了答案,这个问题使用了popen,但也可以让我实时查看输出。以下是我最终得出的结论:

import subprocess
import sys

cmd = ['/media/sf_git/test-automation/src/SalesVision/mswm/shell_test.sh', '4', '2']
print('running command: "{0}"'.format(cmd))  # output the command.
# Here, we join the STDERR of the application with the STDOUT of the application.
process = subprocess.Popen(cmd, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
    line = line.replace('\n', '')
    print(line)
    sys.stdout.flush()
process.wait()                   #  Wait for the underlying process to complete.
errcode = process.returncode      #  Harvest its returncode, if needed.
print( 'Script ended with return code of: ' + str(errcode) )

这将使用Popen并允许我查看所调用脚本的进度。

谢谢。这可以工作,但有一个限制-在命令完成之前,您无法看到任何输出。我从另一个问题中找到了答案,这个问题使用了popen,但也可以让我实时查看输出。我的结局是:啊。忽略我之前的评论。我是瘸子-我不知道如何在注释中发布代码。如果cmd生成足够的输出,那么您的代码就会死锁。如果使用subprocess.pipe,则应排空管道。删除进程。在进程之前等待。通讯。谢谢。这可以工作,但有一个限制-在命令完成之前,您无法看到任何输出。我从另一个问题中找到了答案,这个问题使用了popen,但也可以让我实时查看输出。我的结局是:啊。忽略我之前的评论。我是瘸子-我不知道如何在注释中发布代码。如果cmd生成足够的输出,那么您的代码就会死锁。如果使用subprocess.pipe,则应排空管道。删除process.wait before process.communicate.note:在Python 3.3上,您可以使用for line in process.stdout:而不是iter。。。。另外,您可以使用printline,end=,flush=True代替line.replace'\n',;印刷线;sys.stdout.flush或use line=line.rstrip'\n'。在循环后调用process.stdout.close。注意:在Python 3.3上,可以使用for line in process.stdout:而不是iter。。。。另外,您可以使用printline,end=,flush=True代替line.replace'\n',;印刷线;sys.stdout.flush或use line=line.rstrip'\n'。循环结束后调用process.stdout.close。