I need to download around 200 files from 200 urls in a very short time and very frequently. After countless attempts i came up with the code which is capable of downloading all files in a few seconds. Now i need to make sure that every file will get downloaded, no matter what. So in case of an error, i need to retry for one specific url (or all if it is not possible otherwise)
Common Errors are:
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host ssl:None [None]
aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport
File "C:/test/test.py", line 14, in main
loop.run_until_complete(downloader.download_data_async())
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 584, in run_until_complete
return future.result()
File "C:\test\downloader.py", line 25, in download_data_async
responses = await asyncio.gather(*tasks)
File "C:\test\downloader.py", line 54, in fetch_async
async with session.get(url) as response:
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\client.py", line 1005, in __aenter__
self._resp = await self._coro
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\client.py", line 476, in _request
timeout=real_timeout
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\connector.py", line 522, in connect
proto = await self._create_connection(req, traces, timeout)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\connector.py", line 854, in _create_connection
req, traces, timeout)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\connector.py", line 992, in _create_direct_connection
raise last_exc
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\connector.py", line 974, in _create_direct_connection
req=req, client_error=client_error)
File "C:\Users\Michael\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\connector.py", line 931, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host steamcommunity.com:443 ssl:None [None]
I have already tried surrounding the 'def fetch_async' with a try catch phrase, which did not work out.
from aiohttp import ClientSession
import asyncio
async def fetch_async(url, session):
async with session.get(url) as response:
return await response.text()
async def download_data_async():
tasks = []
#List of urls to download
urls = [...]
async with ClientSession() as session:
for url in urls:
task = asyncio.ensure_future(self.fetch_async(url,session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
return responses