I am using aiohttp
to download some image files asynchronously. my code is:
async def save_message_images(self, message_data, channel):
async with aiohttp.ClientSession() as session:
image_links = []
for i, link in enumerate(message_data.get('image_links')):
async with session.get(link) as response:
if response.status == 200:
file_name = 'media/messages_images/' + str(channel.telegram_username) + '_' + \
str(message_data.get('message_id') + '_' + str(i)) + '.jpg'
file = open(file_name, mode='wb')
file.write(await response.read())
file.close()
image_links.append(file_name)
else:
raise MessageWebCrawler.RetrieveUrlException()
message_data['image_links'] = image_links
await session.close()
return message_data
I run this code in a while
block, but after some iteration aiohttp
gets stuck and does not work anymore. I am using python 3.7.4
and aiohttp 3.5.4
in ubuntu 16
. Can anyone help me please?