1

I have a python program that with time slows to a crawl. I've tested thoroughly, and narrowed it down to a method that downloads an image. The method uses cstringIO and urllib. The problem may also be some sort of infinite download with urllib (the program just freezes after a few hundred downloads).

Any thoughts on where the issue may be?

        foundImages = []

        images = soup.find_all('img')
        print('downloading Images')

        for imageTag in images:
            gc.collect()

            url = None
            try:

                #load image into a file to determine size and width
                url = imageTag.attrs['src']
                imgFile = StringIO(urllib.urlopen(url).read())
                im = Image.open(imgFile)
                width, height = im.size

                #if width and height are both above a threshold, it is a valid image
                #so add to recipe images
                if width > self.minOptimalWidth and height > self.minOptimaHeight:
                    image = MIImage({})
                    image.originalUrl = url.encode('ascii', 'ignore')
                    image.width = width
                    image.height = height

                    foundImages.append(image)

                imgFile = None
                im = None
            except Exception:
                print('failed image download url: ' + url)
                traceback.print_exc()
                continue

        #set the main image to be the first in the array
        if len(foundImages) > 0:
            first = foundImages[0]
            recipe.imageUrl = first.originalUrl

        return foundImages
William Falcon
  • 9,813
  • 14
  • 67
  • 110

0 Answers0