回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>当我上传一个4.8mb的文件时,我的系统内存使用量从30mb跳到了300mb+。在</p>
<p>基本上,一个用户上传一个(例如)4.8mb的jpeg,然后上传到webfiction,然后存储在amazons3存储桶中。在</p>
<p>我想有一件事让我很痛苦,那就是我用easy_缩略图生成了3个文件,这些文件也存储在S3 bucket中。在</p>
<p><strong>更新2:</strong>在这一点上,我认为我的主要问题是内存峰值,但从未释放。我要开始研究跑步gc.收集()在Photo.objects.create()已完成。看起来sorl缩略图可能是一个更好的选择,听起来它更适合于远程存储。在</p>
<p><strong>更新1:</strong>我正在使用django调试工具栏并让它拦截重定向。我现在得到了一些有用的数据。它告诉我我已经执行了20个db查询(eek),但更糟糕的是(我相信)boto(我用于文件存储的东西)记录了124条消息。它看起来像是分别发送每个文件。也许这是正常的,也许不是?不管怎样,它看起来都很高。在</p>
<p>一旦上传了文件,内存永远不会恢复,除非我重置apache。在</p>
<p>这是我的模型:</p>
<pre><code>from django.db import models
from easy_thumbnails.fields import ThumbnailerImageField
from django.conf import settings
import datetime
import os
import string
from django.core.urlresolvers import reverse
...
class Photo(models.Model):
"""
A photo belongs to a user. A photo has a preview size and the original which
is referenced when printing or generating the PDF for print.
"""
def original_resolution(instance, filename):
"""
Returns a path to upload the image to. The path is created with the
website's slug and the current year. Using Amazon S3 for CDN storage
"""
today = datetime.datetime.now()
return 'uploads/{0}/{1}/{2}/{8}-{3}-{4}-{5}-{6}-{7}'.format(
instance.owner.pk,
today.year,
today.month,
today.day,
today.hour,
today.minute,
today.second,
clean_filename(filename),
'original')
def thumbnail_resolution(instance, filename):
"""
Returns a path to upload the image to. The path is created with the
website's slug and the current year. Using Amazon S3 for CDN storage
"""
today = datetime.datetime.now()
return 'uploads/{0}/{1}/{2}/{8}-{3}-{4}-{5}-{6}-{7}'.format(
instance.owner.pk,
today.year,
today.month,
today.day,
today.hour,
today.minute,
today.second,
clean_filename(filename),
'thumbnail')
def editor_resolution(instance, filename):
"""
Returns a path to upload the image to. The path is created with the
website's slug and the current year. Using Amazon S3 for CDN storage
"""
today = datetime.datetime.now()
return 'uploads/{0}/{1}/{2}/{8}-{3}-{4}-{5}-{6}-{7}'.format(
instance.owner.pk,
today.year,
today.month,
today.day,
today.hour,
today.minute,
today.second,
clean_filename(filename),
'editor')
height = models.PositiveIntegerField(blank=True)
width = models.PositiveIntegerField(blank=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
original = ThumbnailerImageField(
upload_to=original_resolution,
resize_source=dict(size=(0, 3100), crop="scale", quality=99),
height_field='height',
width_field='width',
verbose_name=u'Choose Photo')
thumbnail = ThumbnailerImageField(
upload_to=thumbnail_resolution,
resize_source=dict(size=(0, 100), crop="scale"),
blank=True,
null=True)
editor = ThumbnailerImageField(
upload_to=editor_resolution,
resize_source=dict(size=(0, 1000), crop="scale"),
blank=True,
null=True)
def get_absolute_url(self):
return reverse('photos:proxy_editor_image', kwargs={
'pk': self.pk})
def __unicode__(self):
return "Photo #{}".format(self.pk)
...
</code></pre>
<p>下面是我处理上传的视图:</p>
^{pr2}$
<p>我被难住了,我可能只是不明白什么。帮忙吗?在</p>
<p>我已经为这个内存使用问题挣扎了几个月,终于把它缩小到这个范围。在</p>
<p>FWIW,我使用django1.6python2.7,webfiction托管,amazons3存储桶。在</p>