0

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?

sanyassh
  • 8,100
  • 13
  • 36
  • 70
hamid
  • 694
  • 1
  • 8
  • 20
  • did find any solution for this? – d02d33pak Mar 01 '20 at 12:18
  • @d02d33pak I do not exactly remember what I did, but I think it was not aiohttp problem. I think the main problem was that I was running the program in the daemon which stopped after some minutes or some hours. I used `screen` in ubuntu and everything works now. – hamid Mar 01 '20 at 12:29

0 Answers0