multiprocessing 方式
#!/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()#!/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)