django-background-tasks icon indicating copy to clipboard operation
django-background-tasks copied to clipboard

Cannot start repeated background task

Open godardt opened this issue 6 years ago • 2 comments

Hello, I'm trying to use this lib to pull some data from an API endpoint every 5 sec and insert this data in my db. So far I'm using a toy function with a logger just to see if it works, but I tried a lot of things and my background task never triggers...

I'm using Django 3.0.2, DjangoRestFramework 3.11.0 and version 1.2.5 of this lib. So far I have: api.tasks.py:

from background_task import background
from logging import getLogger

logger = getLogger(__name__)


@background
def demo_task(message):
    print("Print without logger")
    logger.debug('demo_task. message={0}'.format(message))

api.apps.ApiConfig

from django.apps import AppConfig


class ApiConfig(AppConfig):
    name = 'api'

    def ready(self):
        from api import tasks

        tasks.demo_task("hello", repeat=5)

But the content of my demo_task is never reached although my debugger stops on the tasks.demo_task("hello", repeat=5) line. I did import properly django_tasks, I added it properly to the settings and I tried a lot of different things, even trying to trigger the task from an API calls, but in fact, the background task is never triggered. Thanks for your help!

godardt avatar Jan 04 '20 06:01 godardt

Hi had a similar issue, do you have started "python manage.py process_tasks" as a process in your terminal?

I thought it would work without this statement always running, but i guess this i wrong. It have to run as process.

FlamingBiFi avatar Jan 13 '20 22:01 FlamingBiFi

Yes, I noticed the requirement to run process_tasks from the terminal... makes no sense to me why there is a different process for running tasks - and of course, why it runs in a different process.

Is that just because of pythons lack of multi threading?

It does mean that I am unable to change any meaningful state on the django server (apart from state stored in the db of course).

michaelwiles avatar Jan 23 '20 15:01 michaelwiles