Search code examples
djangocelerydjango-celery-beat

how to create custom periodic task in celery in django


I want to create a shedular while a API was getting hit after that i have to create the after that i have to stop the shedular in another api was get hitting

API1 --> trigger the celery cron for 3 min periodic task

API2 --> stop the celery periodic task how


Solution

  • [django_celery_beat][1]
    
    from django_celery_beat.models import PeriodicTask, IntervalSchedule
    
    # executes every 10 seconds.
    >>> schedule, created = IntervalSchedule.objects.get_or_create(
    ...     every=10,
    ...     period=IntervalSchedule.SECONDS,
    ... )
    
    >>> PeriodicTask.objects.create(
    ...     interval=schedule,                  # we created this above.
    ...     name='Importing contacts',          # simply describes this periodic task.
    ...     task='proj.tasks.import_contacts',  # name of task.
    ... )
    

    I use this package in my project, you can try this.