我正在使用子进程模块启动子进程并连接到它的输出流(stdout).我希望能够在其标准输出上执行非阻塞读取.有没有办法让.readline非阻塞或在我调用之前检查流上是否有数据.readline?我希望这是可移植的,或至少在Windows和Linux下工作.
这是我现在如何做到的(.readline如果没有数据可用,则阻止它):
p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
output_str = p.stdout.readline()
Run Code Online (Sandbox Code Playgroud) 我正在使用python脚本作为流体动力学代码的驱动程序.当运行模拟时,我subprocess.Popen用来运行代码,从stdout和stderr收集输出到subprocess.PIPE---然后我可以打印(并保存到日志文件)输出信息,并检查是否有任何错误.问题是,我不知道代码是如何进展的.如果我直接从命令行运行它,它会给我输出关于它在什么时间迭代,什么时间,下一个时间步骤是什么等等的输出.
有没有办法既存储输出(用于记录和错误检查),还产生实时流输出?
我的代码的相关部分:
ret_val = subprocess.Popen( run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
output, errors = ret_val.communicate()
log_file.write(output)
print output
if( ret_val.returncode ):
print "RUN failed\n\n%s\n\n" % (errors)
success = False
if( errors ): log_file.write("\n\n%s\n\n" % errors)
Run Code Online (Sandbox Code Playgroud)
最初我正在run_command通过管道,tee以便副本直接进入日志文件,并且流仍然直接输出到终端 - 但这样我就不能存储任何错误(对我的知识).
编辑:
临时解决方案:
ret_val = subprocess.Popen( run_command, stdout=log_file, stderr=subprocess.PIPE, shell=True )
while not ret_val.poll():
log_file.flush()
Run Code Online (Sandbox Code Playgroud)
然后,在另一个终端,运行tail -f log.txt(st log_file = 'log.txt').
我正在使用Python subprocess.communicate()来从一个运行大约一分钟的进程中读取stdout.
如何stdout以流式方式打印出该流程的每一行,以便我可以看到生成的输出,但在继续之前仍然阻止流程终止?
subprocess.communicate() 似乎立刻提供所有输出.
我正在寻找一种Python解决方案,它允许我将命令的输出保存在文件中,而不会将其从控制台中隐藏.
仅供参考:我问的是tee(作为Unix命令行实用程序)而不是Python intertools模块中具有相同名称的函数.
tee,在Windows下不可用)stderr而努力stdoutstderr = subprocess.STDOUT不起作用.以下是我发现的一些不完整的解决方案:
图http://blog.i18n.ro/wp-content/uploads/2010/06/Drawing_tee_py.png
#!/usr/bin/python
from __future__ import print_function
import sys, os, time, subprocess, io, threading
cmd = "python -E test_output.py"
from threading import Thread
class StreamThread ( Thread ):
def __init__(self, buffer):
Thread.__init__(self)
self.buffer = buffer
def run ( self ):
while 1:
line = self.buffer.readline()
print(line,end="") …Run Code Online (Sandbox Code Playgroud) 我想以下列方式使用子进程模块:
stdout(或者stderr,或者两者一起或分别捕获)我已经用Popen创建了进程,但是如果我使用communication(),那么一旦进程终止,数据就会立刻出现在我面前.
如果我创建一个单独的线程,做了阻塞readline()的myprocess.stdout(使用stdout = subprocess.PIPE)我不明白这种方法的任何行或者,直到进程终止.(无论我设置为bufsize)
有没有办法解决这个不可怕的问题,并且在多个平台上运行良好?
我想将python脚本的子进程'stdout和stdin指向同一个文件.我不知道的是如何使两个来源的线条区分开来?(例如,带有感叹号的stderr行前缀.)
在我的特定情况下,不需要对子进程进行实时监视,执行的Python脚本可以等待其执行结束.
我正在运行一个脚本,通过使用执行许多可执行文件
subprocess.call(cmdArgs,stdout=outf, stderr=errf)
Run Code Online (Sandbox Code Playgroud)
when outf/ errf是None或文件描述符(stdout/的不同文件stderr).
有什么方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?
我试图在Python中找到一种方法来运行其他程序:
这是我到目前为止所得到的...方法1:
def method1(command):
## subprocess.communicate() will give us the stdout and stderr sepurately,
## but we will have to wait until the end of command execution to print anything.
## This means if the child process hangs, we will never know....
proc=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable='/bin/bash')
stdout, stderr = proc.communicate() # record both, but no way to print stdout/stderr in real-time
print ' ######### REAL-TIME ######### '
######## Not Possible
print ' ########## RESULTS ########## …Run Code Online (Sandbox Code Playgroud) 我有一个Linux的Python程序几乎看起来像这样:
import os
import time
process = os.popen("top").readlines()
time.sleep(1)
os.popen("killall top")
print process
Run Code Online (Sandbox Code Playgroud)
该程序挂起在这一行:
process = os.popen("top").readlines()
Run Code Online (Sandbox Code Playgroud)
这种情况发生在保持更新输出的工具中,如"Top"
我最好的考验:
import os
import time
import subprocess
process = subprocess.Popen('top')
time.sleep(2)
os.popen("killall top")
print process
Run Code Online (Sandbox Code Playgroud)
它比第一个(它被砍掉)效果更好,但它返回:
<subprocess.Popen object at 0x97a50cc>
Run Code Online (Sandbox Code Playgroud)
第二次试验:
import os
import time
import subprocess
process = subprocess.Popen('top').readlines()
time.sleep(2)
os.popen("killall top")
print process
Run Code Online (Sandbox Code Playgroud)
和第一个一样.它由于"readlines()"而被绞死
它的返回应该是这样的:
top - 05:31:15 up 12:12, 5 users, load average: 0.25, 0.14, 0.11
Tasks: 174 total, 2 running, 172 sleeping, 0 stopped, 0 zombie
Cpu(s): 9.3%us, …Run Code Online (Sandbox Code Playgroud) 我正在通过Python的子进程模块运行脚本.目前我使用:
p = subprocess.Popen('/path/to/script', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = p.communicate()
Run Code Online (Sandbox Code Playgroud)
然后我将结果打印到stdout.这很好,但由于脚本需要很长时间才能完成,我还希望从脚本到stdout的实时输出.我输出输出的原因是因为我想解析它.