golang写出来的程序为什么那么慢

cnchanghai · · 4246 次点击
``` #!/usr/bin/env python #coding:utf-8 import os import time import threading numlock =threading.RLock() prlock=threading.RLock() zxs=0 class pings(threading.Thread): def __init__(self,num,interval): threading.Thread.__init__(self) self.nums=num self.inter=interval self.thread_stop=False self.ns=0 def run(self): global zxs start=self.nums while start<254 and not self.thread_stop: ret=os.system('ping -c 1 -W 2 172.16.100.%d >/dev/null' % start) if not ret: prlock.acquire() print 'ping 172.16.100.%d ok' % start prlock.release() self.ns +=1 start+=self.inter #print '线程%d结束, 此线程共获得 %d 个在线数据' %(self.nums,self.ns) numlock.acquire() zxs+=self.ns numlock.release() def stop(self): self.thread_stop=True def pingt(): s=254 r=s-1 threads=[] for i in range(1,s): t=pings(i,r) threads.append(t) for i in threads: i.start() for i in threads: i.join() global zxs print zxs,'个ip在线' if __name__=='__main__': pingt() ```
#7
更多评论
你的系统是几核的?另外,并非 goroutine 开的越多,就越快
#1
python 的代码也贴出来看看。
#2