Trying to run background task while simultaneously updating the view in django

Question:

I’m working on a project analysing real time data and plotting it in a graph that updates every 10 seconds.

I’ve been trying to make a django web app of this little program but I’m not sure how to:

  1. Run a background task, continuously analysing data.
  2. Every 10 seconds, push this new data onto the view. Or render this data in the view, without breaking the function that needs to be analysing continuously.

What could I look into?
Thank you.

Edit: spelling

Asked By: vc00174

||

Answers:

You should rethink how data is accessed.

First, your data should be stored somewhere secure. For example, a database (Sqlite comes default and is pretty simple). Create a model and then just have your background task update the dabase as needed.

In addition to your default URI, have second URi which returns the data in JSON form (JsonResponse from django.http is very useful for this). This way, client can query this address and get JSON data in response.

Second, have a JavaScript script run asyncroniously to call the URI above and update elements on the screen as needed.

This way user can stay on the page and have it update without needing to redraw the entire page every 10 seconds.

Note that code is not complete, but to give basic idea and you should not use it as-is (it would most likely fail).

views.py:

from django.shortcuts import render
from django.http import JsonResponse
from django.forms.models import model_to_dict

def index(request):
  return render(request, "index.html")

def data(request):

  query = DataPoints.objects.all()
  datapoints= []
  for datapoint in query:
       datapoints.append(model_to_dict(datapoint ))
  data = {
        'datapoints' : datapoints
  }
  return JsonResponse(data, safe=False)

In the index.html, have a script

function fetch_data() {
  fetch('youraddress.com/data') //Fetch data from the address
  .then(response => response.json()) //get the JSON section from the response
  .then( updatePage(data) ); //Here you do whatever you need to update then page with the data
  .then(setTimeout(fetch_data(), 10000)); //Finally, set timeout for 10 seconds and call function again
}

Now you only need to call fetch_data once after the page has loaded and it will for every 10 seconds.

Answered By: Mandemon

@Mandemon answer solve the problem but this is not a good practice to solve in this way.

You can use Web-sockets in your project if new data available server will send automatically to user view (Web browser) where you update your view (HTML).
Making request again and again to server this is not a good idea.


You can implements web-sockets using Django Channels .
Now you can see real time data feed.


Further Reading :

What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?