Skip to main content
Author:

Gregory Terzian

License:

BSD

A package of Django apps for common async tasks using Celery. For now only one app, used for saving an uploaded image with a Celery worker.

async_image_save

As you cannot pass as image to a Celery task, this app deconstructs the UploadedFile instance, passes it to a Celery task and reconstructs it there. Finally saving the uploaded image.

License

All code is under a BSD-style license, see LICENSE for details.

Source: https://github.com/gterzian/django_async.git

Requirements

  • python >= 2.7

  • django >= 1.4

  • Celery >= 3.0

  • PIL >= 1.1.6

Installation

To install run:

pip install django-async-gt

Configuration

You first of all need to have django-celery set up in your project.

settings.py

Add to INSTALLED_APPS:

'async_image_save'

The Problem: Celery tasks cannot accept images as arguments

Django uploads images and other files in the form of UpLoadedFile objects. UpLoadedFile is the abstract baseclasse, while TemporaryUploadedFile and InMemoryUploadedFile are the built-in concrete subclasses. An UploadedFile object behaves somewhat like a file object and represents some file data that the user submitted with a form.

The uploadedfile is received in the view as part of request.FILES, which you will usually bind to a form. Running the form’s is_valid method will then validate this file, or in the case of an ImageField whether the fiel is an actual image, and return the UpLoadedFile object for you to use, as either a TemporaryUploadedFile or InMemoryUploadedFile.

Once Django form validation has been succesfully run, you can safely assume that you are dealing with an actual image. The normal course of business is to immediatly bind the uploaded file object to a model instance, and saving the instance to the database. This means saving the image to your data store while the client is still waiting for a response from the server, which takes a few seconds, especially if you are using S3.

Wouldn’t it be better to pass the image along to a celery worker to save in the background?

The problem is that Celery needs to be able to pickle objects to pass them along to workers, and it cannot pickle a file like object such as an image.

The Solution: de- and reconstruct the image

The remaining option is to deconstruct the file object, write it’s data into a string and taking all the other info that you need. This data can be pickled and therefore passed on to Celery. You then simply need to reconstruct an actual TemporaryUploadedFile or InMemoryUploadedFile object on the other end, and bind this object to an instance of a model, by passing the id of that instance along with all other raw file ‘data’. You can then finally save the model instance.

Read the Django docs

UploadedFile: https://docs.djangoproject.com/en/1.4/topics/http/file-uploads/#django.core.files.uploadedfile.UploadedFile

Binding data to forms: https://docs.djangoproject.com/en/1.4/ref/forms/api/#binding-uploaded-files

How to use it in your project

The functionalities of this app reside in the save_image function, to be used in your views like the below. The function deconstructs the image and sends the data to the async_save task. Please not that the task clears the cache. This is necessary in my opinion because just adding the image to the instance doesn’t invalidate the cache like creating a new instance would, therefore the saved image is not seen by the user until after the cache time out. Therefore clearing the cache in the task seems necessary.

from async_image_save.utils import save_image

def example_view(request):
    if request.method == "POST":
        form = YourModelForm(request.POST, request.FILES)
        if form.is_valid():
            # assuming your model has a main_photo ImageField
            # save the instance without an image
            instance = form.save(commit=False)
            instance.main_photo = None
            instance.save()
            instance.users.add(request.user.userprofile)
            # send the image to be saved by a worker
            save_image(form.cleaned_data['main_photo'], instance)

            return HttpResponseRedirect(reverse('home'))
    else:
        form = YourModelForm()
    context['form'] = form
    return render_to_response("home.html", context, context_instance=RequestContext(request))

Metadata

Release files for django-async-gt 0.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-async-gt 0.6.2
File Size Uploaded Provenance Actions
django-async-gt-0.6.2.tar.gz 4.7 kB Without provenance Details for django-async-gt-0.6.2.tar.gz

Release files / django-async-gt-0.6.2.tar.gz

Download URL django-async-gt-0.6.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
350993aa13712b5842b5c56c09258e33484e77e56efe94c7b881557afafc77f7
BLAKE2b-256 checksum
How to use checksums
35cc09f6d1784e36444263f0268e2d05b8e5ef1c60f828a6b0cc3695cc1220e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-async-gt 0.6.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page