'Run periodic task defined from the view | Flask & Celery

I'm trying to run a simple periodic task every 10 seconds using flask and celery with the following code in my controllers.py:

@celery.task()
def print_hello(word):
    print(f"Hello {word} !")


@celery.on_after_configure.connect
def add_periodic(word, **kwargs):
    celery.add_periodic_task(10.0, print_hello.s(word), name='add every 10')

add_periodic is called from my view.py this way :

from .controllers import add_periodic

@api.route("scheduler")
class DataSchedule(Resource):
    def get(self):
        word = request.args.get("word")
        return add_periodic(word)

My celeryconfig.py is the following :

CELERY_IMPORTS = ('api.controllers')
CELERY_TASK_RESULT_EXPIRES = 30
CELERY_TIMEZONE = 'UTC'

Redis is running OK. Celery beat is started with the fllowing command celery beat -A app.celery :

celery beat v4.4.7 (cliffs) is starting.

Celery worker is giving fine signal starting using command celery worker -A app.celery --loglevel=info --pool=solo :

[2022-05-13 14:30:49,848: INFO/MainProcess] Connected to redis://localhost:6379/1
[2022-05-13 14:30:49,858: INFO/MainProcess] mingle: searching for neighbors
[2022-05-13 14:30:50,906: INFO/MainProcess] mingle: all alone
[2022-05-13 14:30:50,952: INFO/MainProcess] celery@W10P-WORK ready.

But no output for the scheduling. Any help please ? How to fix ? and what is wrong with this ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source