python 同时打开多个api

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值