parallelization of downloading thousands of files using wget


I have thousands of the files like below to be downloaded.

urls = ['', '', '', '', '', '', '', '', '', '']

I can download them one by one using wget as follows.

#wget is here,

import os, glob, subprocess
import itertools
import multiprocessing as mp
import concurrent.futures

header = "authorizationkey"
rd = '.\results\'
for url in urls:
    app = r"C:UsersdanielDownloadswget.exe"[app, 
                     '--header', header,
                     '-P', rd, url])

However, downloading one by one is very slow. So, I wanted to use parallelization or multithreading approach. I tried it as follows.

def doers(urls):
    for url in urls:
        app = r"C:UsersdanielDownloadswget.exe"[app, 
                         '--header', authorization,
                         '-P', rd, url])
def batched(iterable, n):
    it = iter(iterable)
    while (batch := tuple(itertools.islice(it, n))):
        yield batch

num_cpus = mp.cpu_count() - 1 

with mp.Pool(num_cpus) as pool:, batched(urls, num_cpus))

However, my pc just hangs and does not produce any results.
Can someone help me ?

Asked By: daniel



Multithreading is probably optimum for this. You don’t need to use wget if you have the requests module installed.

You may want to consider reducing the number of concurrent threads (ThreadPoolExecutor constructor) as you’re likely to "flood" your network as you stream these fairly large files.

Something like this:

import requests
import os
from concurrent.futures import ThreadPoolExecutor
import sys

HEADER = {AuthorizationKey}
CHUNK = 16 * 1024
TARGET = '/Volumes/G-Drive/results'

def process(url):
        with requests.get(url, headers=HEADER, stream=True) as r:
            *_, filename = url.split('/')
            os.makedirs(TARGET, exist_ok=True)
            with open(os.path.join(TARGET, filename), 'wb') as hdf:
                for chunk in r.iter_content(chunk_size=CHUNK):
    except Exception as e:
        print(e, file=sys.stderr)

def main():
    urls = [
    with ThreadPoolExecutor() as tpe:, urls)

if __name__ == '__main__':
Answered By: DarkKnight

wanted to use parallelization

This is what aria2c aims to do. After reading aria2c man page I suggest you use it following way

aria2c.exe --header="authorizationkey" -i urls.txt

where urls.txt is list of URLs to download, 1 URL per line. If it will work fast enough then fine, if not you might try tinkering with --max-concurrent-downloads=<N>

Answered By: Daweo

As an alternative, you can use AIOHTTP if you prefer an asyncIO interface:

import asyncio
import aiohttp as aio

async def download(url: str, session: aio.ClientSession):
    async with session.get(url) as response:
        data = await
        # Write the data to an HDF file

async def main():
    urls = [...]

    connector = aio.TCPConnector(limit_per_host=32)

    async with aio.ClientSession(connector=connector) as session:
        await asyncio.gather(*[download(url, session) for url in urls])

if __name__ == "__main__":

You can control the maximum number of concurrent requests (per host or global) via the aiohttp.TCPConnector. AIOHTTP caches DNS resolutions by default and using a single ClientSession for all the requests may improve the overall performance.

There are also API’s to stream the received data in chunks if the data is big.

Answered By: Louis Lac