【Python】使用 multiprocessing.dummy 执行多线程任务
2015-09-26 22:11
656 查看
# -*- coding: utf-8 -*- # from multiprocessing import Pool 多进程 from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 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/' ] # 单线程 start = time.time() results = map(urllib2.urlopen, urls) print 'Normal:', time.time() - start # 多线程 start2 = time.time() # 开4个 worker,没有参数时默认是 cpu 的核心数 pool = ThreadPool(4) # 在线程中执行 urllib2.urlopen(url) 并返回执行结果 results2 = pool.map(urllib2.urlopen, urls) pool.close() pool.join() print 'Thread Pool:', time.time() - start2
相关文章推荐
- python - bing_api_search
- 我的python学习笔记(2) - GDAL与Python
- Python 安装boto boto3
- mysql-python 安装错误: Cannot open include file: 'config-win.h': No such file or directory
- 学习python处理python编码问题
- python编码
- 使用 Python 连接 Caché 数据库
- 【python】自己编写的Set类
- Python __init.py__文件
- python 2 和 python 3
- Python变量和对象
- Python包管理pip工具在Mac OSX10.10上的安装及lxml安装错误的解决办法
- 用python发布一条微博
- 使用 python3 环境开发。
- Python大数据依赖包安装
- 用Python学习统计学基础-4
- 【Python注意事项】如何理解python中间generator functions和yield表情
- python2.7 中使用pip遇到的几个问题
- Python常用模块
- python如何生成exe