5

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
rubik
  • 8,814
  • 9
  • 58
  • 88

3 Answers3

3

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? )

Community
  • 1
  • 1
Spike
  • 5,040
  • 5
  • 32
  • 47
2

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."

VMatić
  • 996
  • 2
  • 10
  • 18
0

There is a service called Pusher that will take care of all the messy parts of Push Notifications in HTML5. They supply a client-side and server-library to handle all the messaging and notifications, while taking care of all the HTML5 Websocket nuances.

aaronlevin
  • 1,433
  • 2
  • 13
  • 18