代码拉取完成,页面将自动刷新
# encoding: utf-8
__author__ = 'zhanghe'
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
from tools import time_log
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..
]
@time_log.time_log
def work(num=2):
# Make the Pool of workers
pool = ThreadPool(num)
# Open the urls in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)
# close the pool and wait for the work to finish
pool.close()
pool.join()
@time_log.time_log
def work2(num=2):
p = ThreadPool(num)
for i in urls:
p.apply_async(urllib2.urlopen, args=(i,))
p.close()
p.join()
if __name__ == '__main__':
work(1)
work(2)
work(4)
work(8)
work2(1)
work2(2)
work2(4)
work2(8)
# 测试结果:
# 方法work开始时间:Thu Apr 30 00:26:21 2015
# 方法work结束时间:Thu Apr 30 00:26:46 2015
# 方法work运行时间:25.28S
# 方法work开始时间:Thu Apr 30 00:26:46 2015
# 方法work结束时间:Thu Apr 30 00:27:04 2015
# 方法work运行时间:18.25S
# 方法work开始时间:Thu Apr 30 00:27:04 2015
# 方法work结束时间:Thu Apr 30 00:27:10 2015
# 方法work运行时间:5.47S
# 方法work开始时间:Thu Apr 30 00:27:10 2015
# 方法work结束时间:Thu Apr 30 00:27:15 2015
# 方法work运行时间:5.16S
# 方法work2开始时间:Thu Apr 30 00:33:56 2015
# 方法work2结束时间:Thu Apr 30 00:34:24 2015
# 方法work2运行时间:27.42S
# 方法work2开始时间:Thu Apr 30 00:34:24 2015
# 方法work2结束时间:Thu Apr 30 00:34:35 2015
# 方法work2运行时间:11.62S
# 方法work2开始时间:Thu Apr 30 00:34:35 2015
# 方法work2结束时间:Thu Apr 30 00:34:42 2015
# 方法work2运行时间:6.87S
# 方法work2开始时间:Thu Apr 30 00:34:42 2015
# 方法work2结束时间:Thu Apr 30 00:34:53 2015
# 方法work2运行时间:11.10S
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。