标签:data cost 小例子 cpu process bin 方式 round ted
#!/usr/bin/env python# Version = 3.5.2# __auth__ = ‘无名小妖‘from multiprocessing.dummy import Pool as ThreadPoolimport timeimport urllib.requesturls = [‘https://www.python.org/‘,‘https://www.yahoo.com/‘,‘https://github.com/‘ ]start = time.time()def open_url(url):print(‘GET {}‘.format(url))response = urllib.request.urlopen(url)results = response.read()print(‘{} bytes received.‘.format(len(results)))for url in urls:open_url(url)print(‘Normal:‘, time.time() - start)start2 = time.time()# 开8个 worker,没有参数时默认是 cpu 的核心数pool = ThreadPool(processes=8)# 在线程中执行 urllib2.urlopen(url) 并返回执行结果pool.map(open_url,urls)print(‘Thread Pool:‘, time.time() - start2)pool.close()pool.join()gevent 方式
#!/usr/bin/env python# Version = 3.5.2# __auth__ = ‘无名小妖‘from urllib import requestimport gevent, timefrom gevent import monkeymonkey.patch_all() # 把当前程序的所有的io操作给我单独的做上标记def f(url):print(‘GET: %s‘ % url)resp = request.urlopen(url)data = resp.read()print(‘%d bytes received from %s.‘ % (len(data), url))urls = [‘https://www.python.org/‘,‘https://www.yahoo.com/‘,‘https://github.com/‘]time_start = time.time()for url in urls:f(url)print("同步cost", time.time() - time_start)async_time_start = time.time()gevent.joinall([gevent.spawn(f, ‘https://www.python.org/‘),gevent.spawn(f, ‘https://www.yahoo.com/‘),gevent.spawn(f, ‘https://github.com/‘),])print("异步cost", time.time() - async_time_start)
标签:data cost 小例子 cpu process bin 方式 round ted
原文地址:http://www.cnblogs.com/wumingxiaoyao/p/7047853.html