summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/find_super.py
diff options
context:
space:
mode:
authormakefu <root@pigstarter.de>2013-01-24 02:12:00 +0000
committermakefu <root@pigstarter.de>2013-01-24 02:12:00 +0000
commit0c1883da9a8667595c528130cb9ddc7b0c4a04b2 (patch)
treedc8748d879c7a3bca167b6dc964f6ee720037d31 /retiolum/scripts/adv_graphgen/find_super.py
parent300eafb32422512ced5466197de058d0d909f9be (diff)
find_super is now using a threadpool
Diffstat (limited to 'retiolum/scripts/adv_graphgen/find_super.py')
-rw-r--r--retiolum/scripts/adv_graphgen/find_super.py39
1 files changed, 24 insertions, 15 deletions
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__":