我正在使用以下现代python模块(如threading
和)重写Dumb Guy的代码Queue
。
import threading, urllib2
import Queue
urls_to_load = [
'http://stackoverflow.com/',
'http://slashdot.org/',
'http://www.archive.org/',
'http://www.yahoo.co.jp/',
]
def read_url(url, queue):
data = urllib2.urlopen(url).read()
print('Fetched %s from %s' % (len(data), url))
queue.put(data)
def fetch_parallel():
result = Queue.Queue()
threads = [threading.Thread(target=read_url, args = (url,result)) for url in urls_to_load]
for t in threads:
t.start()
for t in threads:
t.join()
return result
def fetch_sequencial():
result = Queue.Queue()
for url in urls_to_load:
read_url(url,result)
return result
的最佳时间find_sequencial()
是2秒。最佳时间为fetch_parallel()
0.9秒。
也不说thread
由于GIL在Python中没有用。这是线程在Python中有用的情况之一,因为线程在I / O上被阻塞。如您在我的结果中看到的,并行案例的速度快了2倍。