Advertisement
Mochinov

Untitled

Mar 24th, 2022
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. backend/users/tasks.py
  2.  
  3. from celery.schedules import crontab
  4. from celery import group
  5. from backend.celery import app as celery_app
  6.  
  7. @celery_app.on_after_finalize.connect
  8. def setup_periodic_tasks(sender, **kwargs):
  9. sender.add_periodic_task(5.0, restart_work.s(), name='add every 1 min')
  10.  
  11. @celery_app.task
  12. def test_work():
  13. print('Hellow')
  14.  
  15. jobs = group([
  16. test_work.s(),
  17. ])
  18.  
  19. @celery_app.task
  20. def restart_work():
  21. work = jobs.apply_async()
  22.  
  23.  
  24.  
  25. backend/celery.py
  26.  
  27. import os
  28. from celery import Celery
  29. from django.conf import settings
  30. import django
  31.  
  32. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
  33. django.setup()
  34.  
  35.  
  36. # set the default Django settings module for the 'celery' program and scanning for tasks.
  37. app = Celery('TrafficLight')
  38.  
  39. app.config_from_object('django.conf:settings', namespace='CELERY')
  40.  
  41. app.autodiscover_tasks()
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. __init__.py
  49.  
  50. from backend.celery import app as celery_app
  51.  
  52. __all__ = ('celery_app',)
  53.  
  54.  
  55.  
  56.  
  57.  
  58. backend/settings.py
  59.  
  60.  
  61. CACHES = {
  62. "default": {
  63. "BACKEND": "django_redis.cache.RedisCache",
  64. "LOCATION": f"redis://127.0.0.1:6379",
  65. "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}
  66. }
  67. }
  68.  
  69. CELERY_BROKER_URL = f"redis://127.0.0.1:6379"
  70. CELERY_RESULT_BACKEND = f"redis://127.0.0.1:6379"
  71. CELERY_ACCEPT_CONTENT = ['application/json']
  72. CELERY_TASK_SERIALIZER = 'json'
  73. CELERY_RESULT_SERIALIZER = 'json'
  74. CELERY_TIMEZONE = 'UTC'
  75. CELERY_TASK_TRACK_STARTED = True
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement