1. # -*- coding: utf-8 -*-标签:dummy,www,http,Python,import,python,time,org,多线程 From: https://blog.51cto.com/u_14523369/6125996
2. # from multiprocessing import Pool 多进程
3. from multiprocessing.dummy import Pool as ThreadPool #多线程
4. import time
5. import urllib2
6.
7. urls = [
8. 'http://www.python.org',
9. 'http://www.python.org/about/',
10. 'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
11. 'http://www.python.org/doc/',
12. 'http://www.python.org/download/',
13. 'http://www.python.org/getit/',
14. 'http://www.python.org/community/',
15. 'https://wiki.python.org/moin/',
16. 'http://planet.python.org/',
17. 'https://wiki.python.org/moin/LocalUserGroups',
18. 'http://www.python.org/psf/',
19. 'http://docs.python.org/devguide/',
20. 'http://www.python.org/community/awards/'
21. ]
22.
23. # 单线程
24. start = time.time()
25. results = map(urllib2.urlopen, urls)
26. print 'Normal:', time.time() - start
27.
28. # 多线程
29. start2 = time.time()
30. # 开4个 worker,没有参数时默认是 cpu 的核心数
31. pool = ThreadPool(4)
32. # 在线程中执行 urllib2.urlopen(url) 并返回执行结果
33. results2 = pool.map(urllib2.urlopen, urls)
34. pool.close()
35. pool.join()
36. print 'Thread Pool:', time.time() - start2