我正在尝试实时解析块缓冲程序的输出,这意味着直到进程结束才可用。我需要的只是逐行解析,过滤和管理输出中的数据,因为它可能要运行数小时。
我试图用subprocess.Popen()捕获输出,但是是的,正如您可能猜到的那样,Popen无法管理这种行为,它会一直缓冲直到过程结束。
from subprocess import Popen, PIPE
p = Popen("my noisy stuff ", shell=True, stdout=PIPE, stderr=PIPE)
for line in p.stdout.readlines():
#parsing text and getting data
Run Code Online (Sandbox Code Playgroud)
因此,我发现了pexpect,它可以实时输出输出,因为它将stdout视为文件,或者我什至可以做一个肮脏的技巧来打印文件并在函数外部解析它。但是好吧,即使对于我来说,它也太脏了;)
import pexpect
import sys
pexpect.run("my noisy stuff", logfile=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
但我想这应该是一种更好的pythonic方式,只需像子进程一样管理stdout。普朋呢。我怎样才能做到这一点?
编辑:
运行JF提案:
这是故意的错误审核,大约需要25秒。停止。
from subprocess import Popen, PIPE
command = "bully mon0 -e ESSID -c 8 -b aa:bb:cc:dd:ee:00 -v 2"
p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
for line in iter(p.stdout.readline, b''):
print "inside loop"
print line
print "outside loop"
p.stdout.close()
p.wait()
#$ sudo python SCRIPT.py …Run Code Online (Sandbox Code Playgroud) 我试图解决所有情况的一个简单案例。我正在运行一个子进程来执行某个任务,我不希望它要求 stdin,但在我什至不希望的极少数情况下,它可能会尝试读取。我想防止它在这种情况下挂起。
这是一个经典的例子:
import subprocess
p = subprocess.Popen(["unzip", "-tqq", "encrypted.zip"])
p.wait()
Run Code Online (Sandbox Code Playgroud)
这将永远挂起。我已经尝试添加
stdin=open(os.devnull)
Run Code Online (Sandbox Code Playgroud)
还有这样的..
如果我找到有价值的解决方案,我会发布。足以让我在父进程中收到异常 - 而不是无休止地进行通信/等待。
更新:似乎问题可能比我最初预期的更复杂,子进程(在密码和其他情况下)从其他文件描述符读取 - 例如 /dev/tty 与外壳交互。可能没有我想的那么容易解决..
我遇到了一个问题...有谁知道为什么这段代码挂在 while 循环中。循环似乎没有捕捉到stdout.
working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
line = working_file.stdout.readline()
working_file.stdout.flush()
while working_file != "" :
print(line)
line = working_file.stdout.readline()
working_file.stdout.flush()
Run Code Online (Sandbox Code Playgroud)
脚本挂起,光标在readline()遇到时会闪烁。我不明白为什么。任何人都可以透露一些信息吗?
假设我有一个进程打印出一些类似于这个ruby代码的数据.
1.upto(10) {
|i|
puts i
puts "\n"
sleep 0.6
}
Run Code Online (Sandbox Code Playgroud)
我想要一个生成此过程的python代码,并从中读取数据以将其打印出来.
import os
import sys
cmd = "ruby /Users/smcho/Desktop/testit.rb";
pingaling = os.popen(cmd,"r")
while 1:
line = pingaling.readline()
if not line: break
print line,
sys.stdout.flush()
pingaling.close()
Run Code Online (Sandbox Code Playgroud)
这段代码的问题在于它不会逐个打印数字.似乎python在最后一点打印出所有缓冲的数据.
有没有办法打印出没有缓冲的衍生过程的输出?
我的子流程代码有问题.该subprocess.Popen()工作正常,但是当我试图通过阅读它的输出stdout.read()是没有价值的阅读.
**import os
import signal
import subprocess
import threading
import sys
import commands
print commands.getoutput("hcitool dev")
print 'down'
commands.getoutput('hciconfig hci1 down')
print 'up'
commands.getoutput('hciconfig hci1 up')
commands.getoutput('killall hcitool')
stop = False
ping = subprocess.call('hcitool lescan', shell = False,
stdout=subprocess.PIPE,executable='/bin/bash')
for i in ping.stdout:
print i
def kill():
global stop
stop = True
os.kill(ping.pid, signal.SIGTERM)
threading.Timer(5, kill).start()
#while not stop:
# print 'now in while not loop'
# sys.stdout.write(ping.stdout.read(1))
print 'trying to print stdout'
out, err = …Run Code Online (Sandbox Code Playgroud)