Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:59:22+00:00 2026-05-25T05:59:22+00:00

this snippet will ping an ip address in windows and get output line each

  • 0

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() 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:59:22+00:00Added an answer on May 25, 2026 at 5:59 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this snippet to add to my .emacs that will, on save, strip
I found this snippet that says will only allow certain file types. Will it
This works: var.replace(/[^0-9]+/g, ''); That simple snippet will replace anything that is not a
In this snippet: find( sub { print found . $File::Find::name . \n; }, (.));
This snippet: select Datename(hh,DATEADD(HH, -5, [time])) + ':' + Datename(mi,[time]).... will produce: 11:4 But
I've got this snippet of code that I will be replacing in various places
Is there any difference in both of these snippets: Writing this snippet at the
I need advice on this snippet $text = preg_replace('|(A.*)?A(.*)C|', '$1foo$2bar', $text); This will match
I am using this snippet below to get the filename and extension, which works,
This snippet will remove all the slashes on the end of a url #

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.