昊天SEO

使用 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

1215815

更多参考资料 :

http://chriskiehl.com/article/parallelism-in-one-line/

本文地址官网    新乡网站建设,seo

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址