site_str='bing.com|google.com|www.cnn.com|www.voa.com'
#site_str='baidu.com|163.com|qq.com|fudan.com.cn|sohu.com|sina.com|weibo.com|taobao.com|jd.com'.split('|')
IFS='|' read -a sites <<<"$site_str"
unset IFS
t1=`date +%s`
for(( i=0; i< ${#sites[@]}; i++)); do
(
echo "${sites[i]} is starting";
t1=`date +%s`;
text=$(wget ${sites[i]} -O - 2>/dev/null);
t2=`date +%s`;
timearr[i]=$((t2-t1));
printf "%-15s --> %-15s seconds\n" "${sites[i]}" "${timearr[i]}"
) &
done; wait
t2=`date +%s`
echo "actually time used: $((t2-t1))"
-------output----------
#site_str='baidu.com|163.com|qq.com|fudan.com.cn|sohu.com|sina.com|weibo.com|taobao.com|jd.com'.split('|')
www.voa.com is starting
www.cnn.com is starting
google.com is starting
bing.com is starting
www.cnn.com --> 9 seconds
www.voa.com --> 12 seconds
google.com --> 16 seconds
bing.com --> 23 seconds
actually time used: 23
|
#todo
-------output----------
|
import threading
import re, urllib
import time
class WebpageThread(threading.Thread):
def __init__(self, site):
super(WebpageThread,self).__init__()
self.site=site
self.total=0
def run(self):
t1=time.time()
#print "--> accessing to %s ..." % self.site
u=urllib.urlopen("http://"+ self.site)
text = u.read()
#print "--> done with %s " % self.site
self.total = time.time() - t1
sites = 'bing.com|google.com|yahoo.com|www.cnn.com|www.voa.com|www.youtube.com'.split('|')
#sites = 'baidu.com|163.com|qq.com|fudan.com.cn|sohu.com|sina.com|weibo.com|taobao.com|jd.com'.split('|')
threadlst = [];
t1=time.time()
for i in xrange(0,len(sites)):
threadlst.append(WebpageThread(sites[i]))
for i in xrange(0,len(sites)):
threadlst[i].start()
for i in xrange(0,len(sites)):
threadlst[i].join()
total=0
for i in xrange(0,len(sites)):
print "%-15s --> %-15s seconds" % (threadlst[i].site, threadlst[i].total)
total += threadlst[i].total
actual = time.time() - t1
print "total time used: %d" % total
print "actual time used: %d" % actual
-------output----------
#sites = 'baidu.com|163.com|qq.com|fudan.com.cn|sohu.com|sina.com|weibo.com|taobao.com|jd.com'.split('|')
bing.com --> 17.1824159622 seconds
google.com --> 14.7786750793 seconds
yahoo.com --> 14.7702310085 seconds
www.cnn.com --> 14.7730720043 seconds
www.voa.com --> 14.7729201317 seconds
www.youtube.com --> 17.1578299999 seconds
total time used: 93
actual time used: 17
|