使用 multiprocessing.dummy 执行多线程任务
- # -*- 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
使用 multiprocessing.dummy 执行多线程任务的更多相关文章
- python进程池multiprocessing.Pool和线程池multiprocessing.dummy.Pool实例
进程池: 进程池的使用有四种方式:apply_async.apply.map_async.map.其中apply_async和map_async是异步的,也就是启动进程函数之后会继续执行后续的代码不用 ...
- multiprocessing.dummy
昨晚发现放在腾讯云主机上通过crontab定时执行用以爬去斗鱼分类页面数据的爬虫在执行的时候速度特别慢,于是想通过多线程来提高效率. 打开浏览器,键入关键字"python 多线程" ...
- Python之路(第四十六篇)多种方法实现python线程池(threadpool模块\multiprocessing.dummy模块\concurrent.futures模块)
一.线程池 很久(python2.6)之前python没有官方的线程池模块,只有第三方的threadpool模块, 之后再python2.6加入了multiprocessing.dummy 作为可以使 ...
- Oracle定时器执行多线程
what里面加下面代码强制执行多线程 begin execute immediate 'alter session force parallel dml parallel 16'; pkg_s ...
- C# -- 使用 Task 执行多线程任务
C# -- 使用 Task 执行多线程任务 1. 使用 Task 执行多线程任务 class Program { static void Main(string[] args) { Task task ...
- C# -- 使用线程池 ThreadPool 执行多线程任务
C# -- 使用线程池 ThreadPool 执行多线程任务 1. 使用线程池 class Program { static void Main(string[] args) { WaitCallba ...
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- mysql 执行多线程临时方案
sqr::IDatabase *db=NULL;IDbConnection *conn = NULL;int main(int argc, char* argv[]) { db = GetDataba ...
- 【转】Python中的GIL、多进程和多线程
转自:http://lesliezhu.github.io/public/2015-04-20-python-multi-process-thread.html 目录 1. GIL(Global In ...
随机推荐
- workerman 的回调函数
接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...
- StringBuffer
1.StringBuffer StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串 ...
- java 开发常用的Linux命令
1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...
- [linux]树莓派入手体验和系统安装
背景 一直想捣鼓点什么东西.当看到树莓派的时候,就是它了. 树莓派可以安装Linux系统,而我在工作当中,可以说Linux是一半工作环境.树莓派真是个好东西,这个东西应该在我学习linxu/Unix的 ...
- [Linux]CentOS下安装和使用tmux
前天随意点开博客园,看到了一篇关于tmux的文章 Tmux - Linux从业者必备利器,特意还点进去看了.毕竟Linux对于做游戏服务端开发的我来说,太熟悉不过了.不过我就粗略地看了一眼,就关掉了. ...
- [学习笔记] 七步从Angular.JS菜鸟到专家(3):数据绑定和AJAX [转]
这是"AngularJS - 七步从菜鸟到专家"系列的第三篇. 在第一篇,我们展示了如何开始搭建一个AngularaJS应用.第二篇我们讨论了scope和 $scope 的功能. 通过这整个系列的教程 ...
- linux 系统下开机自动启动oracle 监听和实例 (亲测有效)
[oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listene ...
- Web大文件下载控件更新-Xproer.HttpDownloader
资源下载:cab安装包(x86),cab安装包(x64),xpi安装包,crx安装包,nat安装包,exe安装包,开发文档,根证书,VC库, 更新时间:2016-08-19 版本号:1,2,56, ...
- 技术文档--volley 框架
Volley 框架 参考文档:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73e7e808c027fa ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...