start process in background

Hi Everyone!

I am developing a Thermoguard System using Django. Simulated Temperature coming from random generator, its value is plotted on a graph inTemplate and sametime it’s saved to a csv file.

So far it’s working but not exeactly what I want to do. The temperature is generated and saved in views.py. That means, only after visiting the page in Browser but I want this to start in background immediately after run the project and in Template is only visualization, nothing to with generation or with saving in csv file.

Now my question how to make the temperature generation works indepently in background? I asked Chatgpt and it suggested to use Celery but I found it complicated and not needed at this point.

I hope someone can give a better idea. Thanks a lot

You don’t want to try to do this from within your Django project.

Create an external process for this, and run it outside the context of your Django environment.
Celery is one way to handle it. You could also run this using cron or as a systemd service, or using another process manager such as supervisord or runit.

See the thread at Start another process with Django (along with others that you could find) for some other details and more information about this.

There is some ongoing work to try and improve this in Django. You might find this much easier to get started with than Celery.

Thanks KenWhitesell.
I tried Celery and it worked somehow fine but only for the first run. I mean, after trigering , the task.py generate a random number once and save it in the file . But I want it to continously generate random numbers and save them in text file. I tried Celery Beat and it looks good but it creates a new task everytime. In the template, result.html, ajax get the task.id and then get the value of that task. In the second run , a new task was created but ajax does not know its id so it failed to show the second value and so on.
The current value of celery_var (from tasks.py) must be displayed always on result.html.

Thank you

#task.py
from celery import shared_task
from time import sleep
import random

@shared_task
def background_task():
# Simulate a long-running task
sleep(1)
celery_var = random.uniform(15.0, 30.0)
with open(‘celery.txt’, ‘a’) as file:
# Write some text to the file
file.write(f’{celery_var}\n’)
return celery_var

#views.py
from .tasks import background_task

def trigger_task(request):
background_task.delay() # This will run the task independently
result = background_task.delay()
return JsonResponse({‘task_id’: result.id}) # Return the task ID in the response
# return JsonResponse({‘status’: ‘Task started’})

def get_task_result(request, task_id):
task_result = AsyncResult(task_id)
print("######### result: ", task_result.ready())
if task_result.ready():
result = task_result.result
return JsonResponse({‘status’: ‘completed’, ‘result’: result})
else:
return JsonResponse({‘status’: ‘pending’})

#result.html

Celery Task Status $(document).ready(function() { var taskId = $('#task-id').val(); // Get task ID from hidden input
        if (!taskId) {
            // If no task ID, start a new task
            startTask();
        } else {
            // If task ID exists, check its status
            checkTaskStatus(taskId);
        }

        function startTask() {
            $.ajax({
                url: "{% url 'trigger_task' %}",  // Trigger the task endpoint
                method: 'GET',
                success: function(response) {
                    console.log('Task started. Task ID:', response.task_id);
                    if (response.task_id) {
                        $('#task-id').val(response.task_id);  // Store task ID
                        checkTaskStatus(response.task_id);    // Start checking task status
                    } else {
                        $('#result').text('Failed to retrieve task ID.');
                    }
                },
                error: function() {
                    $('#result').text('Failed to start task.');
                }
            });
        }

        function checkTaskStatus(taskId) {
            $.ajax({
                url: "{% url 'get_task_result' 'dummy_id' %}".replace('dummy_id', taskId),
                method: 'GET',
                success: function(response) {
                    console.log('Task status response:', response);
                    if (response.status === 'completed') {
                        $('#result').text('Task result: ' + response.result);
                    } else {
                        $('#result').text('Task is still processing...');
                        setTimeout(function() {
                            checkTaskStatus(taskId);  // Poll every 2 seconds
                        }, 2000);
                    }
                },
                error: function() {
                    $('#result').text('Failed to retrieve task result.');
                }
            });
        }
    });
</script>

Celery Task Status

Starting task...

#settings.py

Celery Beat settings

CELERY_BEAT_SCHEDULE = {
‘run-background-task-every-10-seconds’: {
‘task’: ‘lynx.tasks.background_task’,
‘schedule’: 10.0, # Task runs every 10 seconds
},
}

CELERY_TIMEZONE = ‘UTC’

The key paragraph in the thread quoted above are these questions:

If this is going to be something running continuously, I wouldn’t use Celery for that. I’d make it some kind of service.

I am simulating measurement coming from a sensor. So it has to run forever till stop it manually.

I thought about a complete another project to generate data and send them to django views.py per UDP for example. Anyway I think django hat a special way to deal with this.