Python StringIO内存

2024-09-30 20:25:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python程序,随着时间的推移会慢慢变慢。我已经进行了彻底的测试,并将其缩小到下载图像的方法。该方法使用cstringIO和urllib。问题也可能是使用urllib进行无限下载(程序在几百次下载后就冻结了)。在

有什么想法吗?在

        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

Tags: andthetoimagenoneurlifurllib