I've got a problem. I'm trying to do an assignment for my college but I'm stuck with asyncio. I've got a problem because the main progress bar(this one which shows how many files already were downloaded) updated only when all files are downloaded. I'm trying to fix it for a couple of hours but I don't have much experience with asyncio. Could somebody help me?
import requests
import random
import time
from tqdm.auto import tqdm
import asyncio
async def download(link):
block_size = 1024
response = requests.get(link[0], stream=True)
total_size_in_bytes= int(response.headers['content-length'])
extension = str('.'+response.headers['content-type'].split("/")[1])
progress_bar = tqdm(total=total_size_in_bytes, unit='b', unit_scale=True, desc=f'FILE \"{link[1]}{extension}\"')
with open(link[1]+extension, 'wb') as file:
for data in response.iter_content(block_size):
file.write(data)
progress_bar.update(len(data))
progress_bar.close()
if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes:
print('Error!')
else:
print(f'File \"{link[1]}{extension}\" downloaded successfully!')
async def download_multiple(links):
tasks = []
for l in links:
task = asyncio.create_task(download(l))
tasks.append(task)
progress_bar = tqdm(asyncio.as_completed(tasks), total=len(tasks), desc="DOWNLOADED: ")
[await f for f in progress_bar]
#progress_bar.close()
async def main():
links = [["https://static.toiimg.com/photo/msid-67586673/67586673.jpg?3918697","jeden"],
["https://static.toiimg.com/photo/msid-67586673/67586673.jpg?3918697","dwa"],
["https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4","trzy"]]
await (download_multiple(links))
await main();
This is how it looks: