Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
chriskiehl.com/article/parallelism-in-one-line
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
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/'
# etc..
]
# Make the Pool of workers
pool = ThreadPool(4)
# Open the urls in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)
#close the pool and wait for the work to finish
pool.close()
pool.join()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question