import urllib2
import time
a='http://tj.gongchang.com/api/purchase/?mark=purchase&secret=7232275'
b='http://tj.gongchang.com/api/productrecommend/?mark=recommend&cateid=8260&secret=7232275'
c='http://tj.yw.gongchang.com/api/supid/?mark=supply&cateid=16&supid=23&cid=2&secret=7232275'
#顺序执行
def test1():
t=time.time()
r1=urllib2.urlopen(a)
r2=urllib2.urlopen(b)
r3=urllib2.urlopen(c)
t2=time.time()
print 'chuanxing',t2-t
test1()
#########################
#进程池
def test2():
from multiprocessing.dummy import Pool
pool=Pool(3)
t2=time.time()
d,e,f=pool.map(urllib2.urlopen,[a,b,c])
t3=time.time()
print 'multiprocessing pool',t3-t2
# print d.read()
# print e.read()
# print f.read()
pool.close()
pool.join()
test2()
##################################
#线程池
def test4():
import threadpool
import urllib2,time
url_data_list=[]
def put_handle(url):
re=urllib2.urlopen(url)
data=re.read()
url_data_list.append((url,data))
def main():
pool=threadpool.ThreadPool(3)
requests = threadpool.makeRequests(put_handle, [a,b,c])
[pool.putRequest(req) for req in requests]
pool.wait()
t=time.time()
main()
# print url_data_list
print 'threadpool',time.time()-t
test4()
转载于:https://my.oschina.net/u/1458120/blog/546755