这是触发Django后台任务的最佳方法吗?

我有一个后台任务,它将每秒不断地向数据库添加数据。

This is my views.py file. tasks is the method that triggers the start of the background task, after receiving a POST request.

from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from logging import getLogger

from .tasks import stream_data

logger = getLogger(__name__)

@csrf_exempt
def tasks(request):
    if request.method == 'POST':
        return _post_tasks(request)
    else:
        return JsonResponse({}, status=405)


def _post_tasks(request):
    message = request.POST['message']
    # Call every second
    stream_data(repeat=1, repeat_until=None)
    return JsonResponse({}, status=302)

And this is my tasks.py file:

from background_task import background
from logging import getLogger
from .models import DbData
from datetime import datetime
import random

logger = getLogger(__name__)

@background(schedule=1)
def stream_data():
    time = datetime.now()
    value = random.uniform(0, 1)
    d = DbData(time=time,
                    type='F',
                    value=value)
    d.save()
    logger.debug('Added TIME:{} ; VALUE:{}'.format(time, value))

要触发它,我只是从命令行运行它:

curl -d message='' http://localhost:8000/data/tasks/

which triggers the tasks function from views.py. Do I actually need to trigger it this way, or is there any way for me to just run the background task as soon as I run

python manage.py运行服务器

Also, I'm seeing in the documentation https://django-background-tasks.readthedocs.io/en/latest/ the parameter MAX_RUN_TIME which I'm not sure, but seems to cap the time a task can be running. Is it possible to set it to run forever? Should I use Celery instead?