简体   繁体   English

RuntimeError-超过最大递归深度

[英]RuntimeError - maximum recursion depth exceeded

i create a function that create a thumbnail when uploading an image. 我创建了一个在上传图像时创建缩略图的功能。 I can upload the image and create the thumbnail out of it. 我可以上传图像并从中创建缩略图。 But the result that i got is the thumbnail was created more than one until and i got the error that i've mention aboved. 但是我得到的结果是缩略图创建了一个以上,直到出现上面提到的错误。

def save(self, *args, **kwargs):
        """
        Make and save the thumbnail for the photo here.
        """
        super(AerialFoto, self).save(*args, **kwargs)
        if not self.make_thumbnail():
            raise Exception('Could not create thumbnail - is the file type valid?')

    def make_thumbnail(self):
        """
        Create and save the thumbnail for the photo (simple resize with PIL).
        """
        THUMB_SIZE = (100,100)
        fh = storage.open(self.image.name, 'rb')
        try:
            image = Image.open(fh)
        except:
            return False

        image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
        fh.close()

        # Path to save to, name, and extension
        thumb_name, thumb_extension = os.path.splitext(self.image.name)
        thumb_extension = thumb_extension.lower()

        thumb_filename = thumb_name + 'thumbs' + thumb_extension

        if thumb_extension in ['.jpg', '.jpeg']:
            FTYPE = 'JPEG'
        elif thumb_extension == '.gif':
            FTYPE = 'GIF'
        elif thumb_extension == '.png':
            FTYPE = 'PNG'
        else:
            return False    # Unrecognized file type

        # Save thumbnail to in-memory file as StringIO
        temp_thumb = StringIO()
        image.save(temp_thumb, FTYPE)
        temp_thumb.seek(0)

        # Load a ContentFile into the thumbnail field so it gets saved
        self.thumbnail.save(thumb_filename, ContentFile(temp_thumb.read()), save=True)
        temp_thumb.close()

Traceback http://dpaste.com/1ZG838R 追溯http://dpaste.com/1ZG838R

save() calls make_thumbnail() , which calls self.thumbnail.save(...) , which ends up calling save() again, and around and around it goes. save()调用make_thumbnail() ,后者调用self.thumbnail.save(...) ,最终再调用一次save() ,然后围绕它进行。

You have to break the loop somewhere. 您必须在某个地方打破循环。 My suggestion: make_thumbnail() shouldn't save anything, it should just create the thumbnail and store it on say self._thumbnail_data= temp_thumb.read(); 我的建议: make_thumbnail()不应该保存任何内容,而应该创建缩略图并将其存储在self._thumbnail_data= temp_thumb.read(); .

Then in the save() function, only call self.make_thumbnail() if self._thumbnail_data isn't already set. 然后在save()函数中,如果尚未设置self._thumbnail_data ,则仅调用self.make_thumbnail() Once you know self._thumbnail_data exists then you can do self.thumbnail.save(thumb_filename, self._thumbnail_data, save=True) . 一旦知道self._thumbnail_data存在,就可以进行self.thumbnail.save(thumb_filename, self._thumbnail_data, save=True)

发生这种情况的原因是save调用make_thumbnailmake_thumbnail调用save并且调用次数sys.getrecursionlimit() RuntimeError-超过最大递归深度引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM