python -- map并发
发布日期:2021-08-26 09:44:23 浏览次数:3 分类:技术文章

本文共 1101 字,大约阅读时间需要 3 分钟。

hot3.png

map本来就简洁

urls = ["http://www.baidu.com", "http://www.google.com.hk"]results = map(urllib2.urlopen, urls)

和for..in..比简洁很多,把结果存到results list里

下面开始本文重点

import urllib2 from multiprocessing.dummy import Pool as ThreadPool urls = [        'http://www.python.org',         'http://www.python.org/about/',        'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',        'http://www.python.org/doc/',        'http://www.python.org/download/',        'http://www.python.org/getit/',        'http://www.python.org/community/',        'https://wiki.python.org/moin/',        'http://planet.python.org/',        'https://wiki.python.org/moin/LocalUserGroups',        'http://www.python.org/psf/',        'http://docs.python.org/devguide/',        'http://www.python.org/community/awards/'        # etc..         ]# Make the Pool of workerspool = ThreadPool(4) # Open the urls in their own threads# and return the resultsresults = pool.map(urllib2.urlopen, urls)#close the pool and wait for the work to finish pool.close() pool.join()

4句完成了并发访问

注:参照 

转载于:https://my.oschina.net/1123581321/blog/193820

转载地址:https://blog.csdn.net/weixin_33881753/article/details/92438249 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS 页面间传值 之 属性传值,代理传值
下一篇:基于DASH的视频分片传输

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月12日 07时29分56秒