1. # -*- coding: utf-8 -*-
  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

使用 multiprocessing.dummy 执行多线程任务的更多相关文章

  1. python进程池multiprocessing.Pool和线程池multiprocessing.dummy.Pool实例

    进程池: 进程池的使用有四种方式:apply_async.apply.map_async.map.其中apply_async和map_async是异步的,也就是启动进程函数之后会继续执行后续的代码不用 ...

  2. multiprocessing.dummy

    昨晚发现放在腾讯云主机上通过crontab定时执行用以爬去斗鱼分类页面数据的爬虫在执行的时候速度特别慢,于是想通过多线程来提高效率. 打开浏览器,键入关键字"python 多线程" ...

  3. Python之路(第四十六篇)多种方法实现python线程池(threadpool模块\multiprocessing.dummy模块\concurrent.futures模块)

    一.线程池 很久(python2.6)之前python没有官方的线程池模块,只有第三方的threadpool模块, 之后再python2.6加入了multiprocessing.dummy 作为可以使 ...

  4. Oracle定时器执行多线程

    what里面加下面代码强制执行多线程   begin  execute immediate 'alter session force parallel dml parallel 16';  pkg_s ...

  5. C# -- 使用 Task 执行多线程任务

    C# -- 使用 Task 执行多线程任务 1. 使用 Task 执行多线程任务 class Program { static void Main(string[] args) { Task task ...

  6. C# -- 使用线程池 ThreadPool 执行多线程任务

    C# -- 使用线程池 ThreadPool 执行多线程任务 1. 使用线程池 class Program { static void Main(string[] args) { WaitCallba ...

  7. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  8. mysql 执行多线程临时方案

    sqr::IDatabase *db=NULL;IDbConnection *conn = NULL;int main(int argc, char* argv[]) { db = GetDataba ...

  9. 【转】Python中的GIL、多进程和多线程

    转自:http://lesliezhu.github.io/public/2015-04-20-python-multi-process-thread.html 目录 1. GIL(Global In ...

随机推荐

  1. workerman 的回调函数

    接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...

  2. StringBuffer

    1.StringBuffer StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串 ...

  3. java 开发常用的Linux命令

    1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...

  4. [linux]树莓派入手体验和系统安装

    背景 一直想捣鼓点什么东西.当看到树莓派的时候,就是它了. 树莓派可以安装Linux系统,而我在工作当中,可以说Linux是一半工作环境.树莓派真是个好东西,这个东西应该在我学习linxu/Unix的 ...

  5. [Linux]CentOS下安装和使用tmux

    前天随意点开博客园,看到了一篇关于tmux的文章 Tmux - Linux从业者必备利器,特意还点进去看了.毕竟Linux对于做游戏服务端开发的我来说,太熟悉不过了.不过我就粗略地看了一眼,就关掉了. ...

  6. [学习笔记] 七步从Angular.JS菜鸟到专家(3):数据绑定和AJAX [转]

    这是"AngularJS - 七步从菜鸟到专家"系列的第三篇. 在第一篇,我们展示了如何开始搭建一个AngularaJS应用.第二篇我们讨论了scope和 $scope 的功能. 通过这整个系列的教程 ...

  7. linux 系统下开机自动启动oracle 监听和实例 (亲测有效)

    [oracle@oracle11g ~]$ dbstartORACLE_HOME_LISTNER is not SET, unable to auto-start Oracle Net Listene ...

  8. Web大文件下载控件更新-Xproer.HttpDownloader

    资源下载:cab安装包(x86),cab安装包(x64),xpi安装包,crx安装包,nat安装包,exe安装包,开发文档,根证书,VC库,   更新时间:2016-08-19 版本号:1,2,56, ...

  9. 技术文档--volley 框架

    Volley 框架 参考文档:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73e7e808c027fa ...

  10. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...