Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic pages with Django & Celery

I have a Celery task registered in my tasks.py file. When someone POST to /run/pk I run the task with the given parameters. This task also executes other tasks (normal Python functions), and I'd like to update my page (the HttpResponse returned at /run/pk) whenever a subtask finishes its work.

Here is my task:

from celery.decorators import task


@task
def run(project, branch=None):
    if branch is None:
        branch = project.branch
    print 'Creating the virtualenv'
    create_virtualenv(project, branch)
    print 'Virtualenv created' ##### Here I want to send a signal or something to update my page
    runner = runner(project, branch)
    print 'Using {0}'.format(runner)
    try:
        result, output = runner.run()
    except Exception as e:
        print 'Error: {0}'.format(e)
        return False
    print 'Finished'
    run = Run(project=project, branch=branch,
                       output=output, **result._asdict())
    run.save()
    return True
like image 350
rubik Avatar asked Nov 11 '11 15:11

rubik


2 Answers

Sending push notifications to the client's browser using Django isn't easy, unfortunately. The simplest implementation is to have the client continuously poll the server for updates, but that increases the amount of work your server has to do by a lot. Here's a better explanation of your different options:

Django Push HTTP Response to users

If you weren't using Django, you'd use websockets for these notifications. However Django isn't built for using websockets. Here is a good explanation of why this is, and some suggestions for how to go about using websockets:

Making moves w/ websockets and python / django ( / twisted? )

like image 99
Spike Avatar answered Sep 23 '22 21:09

Spike


With many years past since this question was asked, Channels is a way you could now achieve this using Django.

Then Channels website describes itself as a "project to make Django able to handle more than just plain HTTP requests, including WebSockets and HTTP2, as well as the ability to run code after a response has been sent for things like thumbnailing or background calculation."

like image 22
VMatić Avatar answered Sep 22 '22 21:09

VMatić