如何清除Python子进程中的stdout?

发布于 2024-12-02 08:21:07 字数 473 浏览 0 评论 0原文

这段代码将在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2024-12-09 08:21:07

由于读取之间有 2 秒超时,ping 生成的行数比您正在读取的行数多得多。我将 os.kill 调用移至另一个线程,并使用主线程读取 proc.stdout 中的每一行:

import sys, os
import subprocess
import threading
import signal
import time

#Use ctrl-c and ctrl-break to terminate the script/ping

def sigbreak(signum, frame):
    import sys
    if proc.poll() is None:
        print('Killing ping...')
        proc.kill()
    sys.exit(0)

signal.signal(signal.SIGBREAK, sigbreak)
signal.signal(signal.SIGINT, sigbreak)

#executes in a separate thread
def run(pid):
    while True:
        time.sleep(2)
        try: 
            os.kill(pid, signal.CTRL_BREAK_EVENT)
        except WindowsError:
            #quit the thread if ping is dead 
            break

cmd = [r'c:\windows\system32\ping.exe', '127.0.0.1', '-l', '10000', '-t']
flags = subprocess.CREATE_NEW_PROCESS_GROUP
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=flags)
threading.Thread(target=run, args=(proc.pid,)).start()

while True:
    line = proc.stdout.readline()
    if b'statistics' in line:
        #I don't know what you're doing with the ping stats.
        #I'll just print them.
        for n in range(4):
            encoding = getattr(sys.stdout, 'encoding', 'ascii') 
            print(line.decode(encoding).rstrip())
            line = proc.stdout.readline()
        print()

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:

import sys, os
import subprocess
import threading
import signal
import time

#Use ctrl-c and ctrl-break to terminate the script/ping

def sigbreak(signum, frame):
    import sys
    if proc.poll() is None:
        print('Killing ping...')
        proc.kill()
    sys.exit(0)

signal.signal(signal.SIGBREAK, sigbreak)
signal.signal(signal.SIGINT, sigbreak)

#executes in a separate thread
def run(pid):
    while True:
        time.sleep(2)
        try: 
            os.kill(pid, signal.CTRL_BREAK_EVENT)
        except WindowsError:
            #quit the thread if ping is dead 
            break

cmd = [r'c:\windows\system32\ping.exe', '127.0.0.1', '-l', '10000', '-t']
flags = subprocess.CREATE_NEW_PROCESS_GROUP
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=flags)
threading.Thread(target=run, args=(proc.pid,)).start()

while True:
    line = proc.stdout.readline()
    if b'statistics' in line:
        #I don't know what you're doing with the ping stats.
        #I'll just print them.
        for n in range(4):
            encoding = getattr(sys.stdout, 'encoding', 'ascii') 
            print(line.decode(encoding).rstrip())
            line = proc.stdout.readline()
        print()
我不吻晚风 2024-12-09 08:21:07

尝试 ping.py 而不是使用 ping.exe

Try ping.py instead of juggling with the ping.exe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文