def api_ping_list(hosts, bind=None, timeout=None, threads=10):
"""
Ping a list of hosts and return a list of their statuses.
"""
if len(hosts) == 0:
return {}
# Work around a bug in 2.6
# TODO: Get rid of this when 2.6 is no longer in the picture.
if not hasattr(threading.current_thread(), "_children"):
threading.current_thread()._children = weakref.WeakKeyDictionary()
pool = multiprocessing.dummy.Pool(processes=min(len(hosts), threads))
pool_args = [(host, timeout) for host in hosts]
result = {}
def ping_one(arg):
host, timeout = arg
up, _ = api_ping(host, bind=bind, timeout=timeout)
return (host, up)
for host, state in pool.imap(
ping_one,
pool_args,
chunksize=1):
result[host] = state
pool.close()
return result
评论列表
文章目录