From 0c1883da9a8667595c528130cb9ddc7b0c4a04b2 Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 24 Jan 2013 02:12:00 +0000 Subject: find_super is now using a threadpool --- retiolum/scripts/adv_graphgen/find_super.py | 39 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'retiolum/scripts/adv_graphgen/find_super.py') diff --git a/retiolum/scripts/adv_graphgen/find_super.py b/retiolum/scripts/adv_graphgen/find_super.py index df01734e..99548f1c 100644 --- a/retiolum/scripts/adv_graphgen/find_super.py +++ b/retiolum/scripts/adv_graphgen/find_super.py @@ -23,23 +23,32 @@ def find_super(path="/etc/tinc/retiolum/hosts"): if addrs : yield (f ,[(addr ,int(port)) for addr in addrs]) -def check_super(path="/etc/tinc/retiolum/hosts"): - from socket import socket,AF_INET,SOCK_STREAM - for host,addrs in find_super(path): +def try_connect(addr): + try: + from socket import socket,AF_INET,SOCK_STREAM + s = socket(AF_INET,SOCK_STREAM) + s.settimeout(2) + s.connect(addr) + s.settimeout(None) + s.close() + return addr + except Exception as e: + pass + #return () + +def check_one_super(ha): + host,addrs = ha valid_addrs = [] for addr in addrs: - try: - s = socket(AF_INET,SOCK_STREAM) - s.settimeout(3) - s.connect(addr) - #print("success connecting %s:%d"%(addr)) - s.settimeout(None) - s.close() - valid_addrs.append(addr) - except Exception as e: - pass - #print("cannot connect to %s:%d"%(addr)) - if valid_addrs: yield (host,valid_addrs) + ret = try_connect(addr) + if ret: valid_addrs.append(ret) + if valid_addrs: return (host,valid_addrs) + +def check_super(path="/etc/tinc/retiolum/hosts"): + from multiprocessing import Pool + p = Pool(20) + return filter(None,p.map(check_one_super,find_super(path))) + if __name__ == "__main__": -- cgit v1.2.3