Source

gevent-playground / examples / multiping_example.py

Full commit
#!/usr/bin/env python
from geventutil import threadpool
import subprocess
import sys
from copy import copy

def run_proc(cmd, shell=False, stdin=None):
    p = subprocess.Popen(cmd, shell=shell, stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    sout, serr = p.communicate(stdin)
    return p.returncode, sout

pool = threadpool.ThreadPool(5)
cmdList = []
for host in sys.argv[1:]:
    # This is solaris ping - which doesn't need arguments :)
    cmdList.append((["/usr/sbin/ping", host],))

async_lst = pool.map_async(run_proc, cmdList)

while async_lst != []:
    threadpool.waitany(async_lst)
    for async in copy(async_lst):
        if async.ready():
            exitcode, output = async.get()
            print output,
            async_lst.remove(async)

sys.stdout.flush()