Сельдерей делает функцию print() не видимой

(portfolio) PS C:\Users\arund\Desktop\Code\Django\portfolio-project> celery -A portfolio beat -l info
celery beat v5.2.3 (dawn-chorus) is starting.
__    -    ... __   -        _
LocalTime -> 2022-03-19 14:41:16
Configuration ->
    . broker -> redis://:**@-//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%INFO
    . maxinterval -> 5.00 minutes (300s)
[2022-03-19 14:41:16,221: INFO/MainProcess] beat: Starting...
[2022-03-19 14:41:16,738: INFO/MainProcess] Scheduler: Sending due task print-message-once-and-then-every-minute (print_time)
[2022-03-19 14:42:00,004: INFO/MainProcess] Scheduler: Sending due task print-message-once-and-then-every-minute (print_time)
[2022-03-19 14:43:00,000: INFO/MainProcess] Scheduler: Sending due task print-message-once-and-then-every-minute (print_time)
[2022-03-19 14:44:00,000: INFO/MainProcess] Scheduler: Sending due task print-message-once-and-then-every-minute (print_time)

Я пытаюсь настроить периодическую задачу с помощью celery, но функция print(), похоже, не видна, даже если она выполняется. В частности, при использовании print_time в tasks.py.

tasks.py

from celery import shared_task
from datetime import datetime
from __future__ import absolute_import, unicode_literals

@shared_task(name = "print_time")
def print_time():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Current Time is ")

celery.py

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
from decouple import config
from celery.schedules import crontab
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portfolio.settings')
app = Celery('portfolio')
app.conf.update(BROKER_URL=config('REDIS_URL'),
                CELERY_RESULT_BACKEND=config('REDIS_URL'))
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

# There are used for periodic request which use tasks.py
app.conf.beat_schedule = {
    #Scheduler Name
    'print-message-once-and-then-every-minute': {
        # Task Name (Name Specified in Decorator)
        'task': 'print_time',  
        # Schedule      
        'schedule': crontab(minute='*/1')
    },
}

init.py

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)

settings.py

# CELERY STUFF
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Vancouver'

Добавьте print_time() в вашу программу.

Вернуться на верх