Use threading! It's super simple. Here's an example. You can change the number of connections to suit your needs.
import threading, Queue
import urllib
urls = [
'http://www.google.com',
'http://www.amazon.com',
'http://www.ebay.com',
'http://www.google.com',
'http://www.amazon.com',
'http://www.ebay.com',
'http://www.google.com',
'http://www.amazon.com',
'http://www.ebay.com',
]
queue = Queue.Queue()
for x,url in enumerate(urls):
filename = "datafile%s-%s" % (x,url)
queue.put((url, filename))
num_connections = 10
class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while 1:
try:
url, filename = self.queue.get_nowait()
except Queue.Empty:
raise SystemExit
urllib.urlretrieve(url,filename.replace('http://',''))
# start threads
threads = []
for dummy in range(num_connections):
t = WorkerThread(queue)
t.start()
threads.append(t)
# Wait for all threads to finish
for thread in threads:
thread.join()