如何清除Python子进程中的stdout?
这段代码将在 Windows 中 ping 一个 ip 地址,并每 2 秒获取一次输出行,但是,我发现 ping.exe 进程在运行后内存增加非常缓慢,如果我将其部署到并行 ping 1000 个 ip,很快就会导致服务器挂起,我认为这可能是因为标准输出缓冲区,我可以知道如何清除标准输出或限制其大小吗?谢谢!
...
proc = subprocess.Popen(['c:\windows\system32\ping.exe','127.0.0.1', '-l', '10000', '-t'],stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
while True:
time.sleep(2)
os.kill(proc.pid, signal.CTRL_BREAK_EVENT)
line = proc.stdout.readline()
this snippet will ping an ip address in windows and get output line each 2 seconds, however, I found there's a very slowly memory increasement of ping.exe process after run it, if I deploy it to ping 1000 ip parallel, soon it will cause server hang, I think it may because of stdout buffer, may I know how to clear the stdout or limit its size? thanks!
...
proc = subprocess.Popen(['c:\windows\system32\ping.exe','127.0.0.1', '-l', '10000', '-t'],stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
while True:
time.sleep(2)
os.kill(proc.pid, signal.CTRL_BREAK_EVENT)
line = proc.stdout.readline()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于读取之间有 2 秒超时,ping 生成的行数比您正在读取的行数多得多。我将 os.kill 调用移至另一个线程,并使用主线程读取 proc.stdout 中的每一行:
ping is producing many more lines than you're reading due to the 2 second timeout between reads. I'd move the os.kill call into another thread, and use the main thread to read every line from
proc.stdout
:尝试 ping.py 而不是使用
ping.exe
Try ping.py instead of juggling with the
ping.exe