Python MultiThreading For Downloads
I need to download 2 diffrent files at the time.
import urllib
urllib.urlretrieve('http://example.com/test1.zip', "C:\Test\test1.zip")
urllib.urlretrieve('http://example.com/test2.zip', "C:\Test\test2.zip")
I need to download them at the same time. Thank you
You should be able to use standard python threading for this. You will need to create a class that represents a separate thread for each download, then start each thread.
Something like:
import urllib
from threading import Thread
class Downloader(Thread):
def __init__(self, file_url, save_path):
self.file_url = file_url
self.save_path = save_path
def run():
urllib.urlretrieve(self.file_url, self.file_path)
Downloader('http://example.com/test1.zip', "C:\Test\test1.zip").start()
Downloader('http://example.com/test2.zip', "C:\Test\test2.zip").start()
Writing this off the top of my head, so not guaranteed to work. The key is to subclass the Thread class and override the run() function, then start the thread.
Regarding Dave_750's answer, threading is actually effective in this situation because the download operation is I/O, not interpreting Python code, so the GIL isn't really an issue. I use threads for sending multiple emails, for example.