Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- backend/users/tasks.py
- from celery.schedules import crontab
- from celery import group
- from backend.celery import app as celery_app
- @celery_app.on_after_finalize.connect
- def setup_periodic_tasks(sender, **kwargs):
- sender.add_periodic_task(5.0, restart_work.s(), name='add every 1 min')
- @celery_app.task
- def test_work():
- print('Hellow')
- jobs = group([
- test_work.s(),
- ])
- @celery_app.task
- def restart_work():
- work = jobs.apply_async()
- backend/celery.py
- import os
- from celery import Celery
- from django.conf import settings
- import django
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
- django.setup()
- # set the default Django settings module for the 'celery' program and scanning for tasks.
- app = Celery('TrafficLight')
- app.config_from_object('django.conf:settings', namespace='CELERY')
- app.autodiscover_tasks()
- __init__.py
- from backend.celery import app as celery_app
- __all__ = ('celery_app',)
- backend/settings.py
- CACHES = {
- "default": {
- "BACKEND": "django_redis.cache.RedisCache",
- "LOCATION": f"redis://127.0.0.1:6379",
- "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}
- }
- }
- CELERY_BROKER_URL = f"redis://127.0.0.1:6379"
- CELERY_RESULT_BACKEND = f"redis://127.0.0.1:6379"
- CELERY_ACCEPT_CONTENT = ['application/json']
- CELERY_TASK_SERIALIZER = 'json'
- CELERY_RESULT_SERIALIZER = 'json'
- CELERY_TIMEZONE = 'UTC'
- CELERY_TASK_TRACK_STARTED = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement