4

I am new to web development, and I am working on a basic image gallery app (learning exercise) using Django. I have it set up so I can upload a zip full of images all at once to create a new album. This all seems to work OK, but I am getting an HTTP 504 error when the uploaded file is especially large.

I gather (please correct me if I am wrong) this error means my app is too slow to return an HTTP response. I am guessing this is because it takes a long time to unzip and process (create a Pic object in the DB and create thumbnails) all the images.

Is there a way to return a response (say to some intermediate page) while still performing the processing in the background - maybe using threads? What is the proper way to handle this? Is it time for me to start learning Javascript/AJAX?

Thank you!


Models:

from django.db import models
from blog.models import Post

class Album(models.Model):
    title = models.CharField(max_length=128)
    slug = models.SlugField()
    description = models.TextField()
    parent = models.ForeignKey('self', null=True, blank=True)

    pub = models.BooleanField()
    date_created = models.DateTimeField(auto_now_add=True)
    date_published = models.DateTimeField(null=True, blank=True)
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.title

class Pic(models.Model):
    image = models.ImageField(upload_to='pics/%Y/%m') 
    title = models.CharField(max_length=128)
    caption = models.TextField(blank=True, null=True)
    albums = models.ManyToManyField('Album', null=True, blank=True)
    posts = models.ManyToManyField(Post, blank=True, null=True)

    date_taken = models.DateTimeField(null=True, blank=True) 
    date_uploaded = models.DateTimeField(auto_now_add=True) 
    date_modified = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.title

View:

I'm doing this manually because I didn't grok the Django admin when I started. I think it might be better to use admin customization here.

def new_album(request):
    if request.method == "POST":
        form = AlbumForm(request.POST, request.FILES)
        if form.is_valid():
            from gallery.pic_handlers import handle_uploaded_album
            pics = handle_uploaded_album(request.FILES['pic_archive'])
            a = form.save()
            a.slug = slugify(a.title)
            a.save()
            for pic in pics:
                pic.albums.add(a)
            return HttpResponseRedirect('/gallery/album/%s/' % a.slug)
    else:
        form = AlbumForm()

    return render_to_response('new_album.html', {
        'form' : form,
    }, context_instance = RequestContext(request))

Additional processing:

def handle_uploaded_album(pic_archive):
    destination = open(join(settings.MEDIA_ROOT,pic_archive.name), 'wb+')
    for chunk in pic_archive.chunks():
        destination.write(chunk)
    destination.close()

    today = datetime.date.today()
    save_path = 'pics/{0}/{1:02}/'.format(today.year, today.month)
    tmp_path = 'tmp/'
    z = zipfile.ZipFile(join(settings.MEDIA_ROOT,pic_archive.name), 'r')
    pics = []
    for member in z.namelist():
        if '/' in member or '\\' in member: 
            # don't deal with any directories inside the zip
            # this also solves the '__MACOSX' issue
            continue
        if splitext(member)[1] in IMG_EXT:
            z.extract(member,join(settings.MEDIA_ROOT,tmp_path))
            im = File(open(join(settings.MEDIA_ROOT,tmp_path,member), 'rb'))
            # create a Pic from this file
            pic = Pic()
            pic.title = member
            pic.image.save(
                join(save_path, member),
                im,
                True)
            create_thumbnails(pic)
            im.close()
            # remove extracted images
            remove(join(settings.MEDIA_ROOT,tmp_path,member))

            # TODO: save date taken if available
            pics.append(pic)

    z.close()
    remove(join(settings.MEDIA_ROOT,pic_archive.name))

    return pics

def create_thumbnails(pic):
    fname, ext = splitext(pic.image.path)
    img = Image.open(pic.image.path)

    img.thumbnail((512,512), Image.ANTIALIAS)
    img.save(fname + '_m' + ext)

    img.thumbnail((128,128), Image.ANTIALIAS)
    img.save(fname + '_s' + ext)
aganders3
  • 5,838
  • 26
  • 30

2 Answers2

5

Long tasks such as this processing take too much time, and your client and/or your proxy will timeout - which is the 504 error you see.

you should not run long jobs this way!

As you correctly ask at the end, you need a way to detach long executions - via an asynchronous queue system, such as celery. This way you can return an answer straight away to your clients, while the backend runs jobs asynchronously.

you should look at one of the followings:

Since django-celery is definitely the best option, next step is to learn about it; there are also a lot of SO questions around it

If you want to be sure it's processing that fails, and not upload, simply try disabling all processing and returning straight away to your client. If it's still failing, you also need to tweak your webserver so that it's not going in timeout!

Community
  • 1
  • 1
Stefano
  • 18,083
  • 13
  • 64
  • 79
1

Ok,so you are receiving a 504 Error, As such you need to understand a bit HTTP Status codes, see here for more info

So, you are receiving a 5xx Error which is generated by the server, the 504 Error specifies

504 Gateway Timeout
    The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

Well, I never program in Django but what I can understand from the details you gave is that the reason for this error is surely the large file. What may b is happening is that when you are uploading the large file, there is a timeout being set, but since the file is large, the time that is being taken to upload the file is exceeding the timeout and thus the error is being generated.

Well, try to google out how to increase the timeout or set a max file size which when uploaded does not exceed the timeout, hopes it helps :-)

Noor
  • 19,638
  • 38
  • 136
  • 254
  • So the error is due to how long it takes to upload the file, not how long it takes to process it after upload? Surely there is a way to avoid this, as many sites allow you to upload large files over HTTP. – aganders3 Jan 09 '12 at 06:24
  • ya of course, I'm more sure that the error is due to the amount of time to upload file – Noor Jan 09 '12 at 06:56
  • 1
    @aganders3 One way of solving this is to split the file into smaller chunks on the client side, and send them using AJAX. – Some programmer dude Jan 09 '12 at 07:20