Skip to content Skip to sidebar Skip to footer

Threading In Django Is Not Working In Production

I have a function in my Django views.py that looks like this. def process(request): form = ProcessForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(com

Solution 1:

Wrong architecture. Django and other web apps should be spawning threads like this. The correct way is to create an async task using a task queue. The most popular task queue for django happens to be Celery.

The mart:processing page should then check the async result to determine if the task has been completed. A rough sketch is as follows.

from celery.result import AsynResult
from myapp.tasks import my_task

...
if form.is_valid():
    ...
    task_id = my_task()
    request.session['task_id']=task_id
    return HttpResponseRedirect(reverse('mart:processing'))
    ...

On the subsequent page

task_id = request.session.get('task_id')
if task_id:
    task = AsyncResult(task_id)

Post a Comment for "Threading In Django Is Not Working In Production"