How to download a file over HTTP with multi-thread (asynchronous download) using Python 2.7

Tags: , , ,

I have a file to download (download path extracted from json. eg: http://testsite/

I need a help to perform, all the 5 threads should download the file to the output directory and the download has to be Asynchronous or concurrent.

Currently with the below code it does download the file 5 times but it downloads one by one (Synchronous).

What I want is, the download to be simultaneous.

def dldr(file=file_url, outputdir=out1):
    local_fn = str(uuid.uuid4())
    if not os.path.exists(outputdir):
    s =
    urllib.urlretrieve(file, outputdir + os.sep + local_fn)
    e =
    time_diff = e - s
    logger(out1, local_fn, time_diff)

for i in range(1, 6):
    t = threading.Thread(target=dldr())

I have read Requests with multiple connections post and it’s helpful, but doesn’t address the requirement of the question asked.


I use threading module for download threads:
Also requests, but you can change that to urllib by yourself.

import threading
import requests

def download(link, filelocation):
    r = requests.get(link, stream=True)
    with open(filelocation, 'wb') as f:
        for chunk in r.iter_content(1024):
            if chunk:

def createNewDownloadThread(link, filelocation):
    download_thread = threading.Thread(target=download, args=(link,filelocation))

for i in range(0,5):
    file = "C:\test" + str(i) + ".png"
    print file
    createNewDownloadThread("", file)

Source: stackoverflow