This is a very naive question, but I feel I don't understand something fundamental about asynchronous/background tasks in django and python.
I try to replicate a simple example provided by django-background-tasks (https://github.com/collinmutembei/django-background-tasks-example) in order to make django perform a background task 60 seconds later than it was actually run. But I guess the same is valid for any other background task manager such as Celery or Huey
The example is pretty simple - as soon as the user accesses the url, a simple function that prints a message is executed without blocking the main django process, 60 sec later
from background_task import background
from logging import getLogger
logger = getLogger(__name__)
@background(schedule=60)
def demo_task(message):
logger.debug('demo_task. message={0}'.format(message))
The problem is that I really don't understand the basics. It doesn't run unless I start a separate (or detached) process python manage.py process_tasks. Should I always do it to make the background task work, or there is a way to do it without starting a parallel process?
If I should start a parallel process, can I do it from inside of django code. Something like:
import subprocess
process = subprocess.Popen(['python', 'manage.py','process_tasks'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)