import subprocess
import time
import threading
stop_output = False
def f(p):
global stop_output
while True:
l = p.stdout.readline()
if not l or stop_output:
break
print(l.rstrip()) # or whatever you want to do with the line
airodump = subprocess.Popen(['airodump-ng','mon0'],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True
)
t = threading.Thread(target=f,args=(airodump,))
t.start()
time.sleep(10)
# kill the process and stop the display after 10 seconds whatever happens
airodump.terminate()
stop_output = True
当同时设置
stdout=subprocess.PIPE
和stderr=subprocess.PIPE
时,存在死锁的风险(除非您使用communicate
同时读取两个通道),因为如果进程写入标准错误,而您试图读取标准输出,则这两个都将永远阻塞。在在您的例子中,您希望控制读取,因此
communicate
不是一个选项。我怀疑你只是想合并两个数据流,所以要改变:通过
^{pr2}$将标准错误重定向到标准输出并获取
o_file.stdout
中的所有output+error顺便说一下:
for i in [1, 2, 3, 4 ,5]:
更像是“python”,比如:for _ in range(5):
,因为您没有使用i
,并且假设您要循环10000次:)但是如果您的应用程序在所有情况下都不是一次打印行,这并不能解决您的问题,因为
read()
是阻塞的,您需要它在您想要的时候完全停止,所以我要:True
,并终止进程像这样:
相关问题 更多 >
编程相关推荐