Dynamic Pages with Django & Celery

I have a Celery task registered in my tasks.py file. When someone POST to / run / pk starts a task with the given parameters. This task also performs other tasks (normal Python functions), and I would like to refresh my page (returned by HttpResponse in / run / pk) whenever the subtask completes 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 
+4
source share
3 answers

Sending push notifications to the client’s browser using Django is not easy, unfortunately. The simplest implementation is for the client to constantly poll the server for updates, but this increases the amount of work your server needs to do a lot. Here is the best explanation of the various options:

Django Push HTTP response to users

If you are not using Django, you should use the websites for these notifications. However, Django is not designed to use websites. Here's a good explanation of why this is, and some tips on how to use websockets:

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

+3
source

Ever since this question was asked, Channels is the way you could achieve this using Django.

The feeds website then describes itself as "a project that allows Django to handle more than just simple HTTP requests, including WebSockets and HTTP2, as well as the ability to run code after sending an answer for things like thumbnail or background calculation."

+1
source

There is a service called Pusher that takes care of all the messy parts of Push Notifications in HTML5. They provide client and server libraries for handling all messages and notifications, and also take care of all the nuances of HTML5 websites.

0
source

Source: https://habr.com/ru/post/1380782/


All Articles