Django celery crontab every 30 seconds - is it even possible?

django-celery beat example
django periodic tasks without celery
celery beat flask
celery periodic_task decorator
django-celery beat one off task
celery periodic task not working
crontab seconds
django-cron

Is it possible to run the django celery crontab very 30 seconds DURING SPECIFIC HOURS?

There are only settings for minutes, hours and days.

I have the crontab working, but I'd like to run it every 30 seconds, as opposed to every minute.

Alternatively...

Is it possible to turn the interval on for 30 seconds, and turn on the interval schedule only for a certain period of the day?

Very first example they have in the documentation is...

Example: Run the tasks.add task every 30 seconds.

from datetime import timedelta

CELERYBEAT_SCHEDULE = {
    "runs-every-30-seconds": {
        "task": "tasks.add",
        "schedule": timedelta(seconds=30),
        "args": (16, 16)
    },
}

Periodic Tasks, The default scheduler (storing the schedule in the celerybeat-schedule file) will python manage.py shell >>> from djcelery.models import PeriodicTask from celery import Celery from celery.schedules import crontab app = Celery() Example: Run the tasks.add task every 30 seconds. Possible event types are:  Using a timedelta for the schedule means the task will be sent in 30 second intervals (the first task will be sent 30 seconds after celery beat starts, and then every 30 seconds after the last run). A Crontab like schedule also exists, see the section on Crontab schedules.

If you really need to do this and you need a solution quickly, you could resort to adding: sleep into your script. (obviously you'd need to add a parameter to your script to setup the sleep time to either 0 or 30 seconds and add the script twice to crontab, once with 0 as the parameter, secondly with 30).

Please don't down vote me on this! It's a solution, albeit I'm making it very clear this is not an ideal solution.

Crontabs in Celery - The Andela Way, It schedules tasks to run at fixed times, dates or even intervals in an elegant, To represent every possible valid value in a keyword argument using an asterisk. crontab(minute="*")# a job is scheduled to run for every minute between 1am and 2am This will be the interface between Python and MySQL. Using a timedelta for the schedule means the task will be executed 30 seconds after celerybeat starts, and then every 30 seconds after the last run. A crontab like schedule also exists, see the section on Crontab schedules.

You could just use

from datetime import timedelta

@periodic_task(run_every=timedelta(seconds=30))
def thirty_second_task():
   now = datetime.now()
   if now.hour == 12 or now.hour == 9:
       your code goes here`

That will run the function every thirty seconds, but it will only run your code if the hour value of the current time is 12 or 9.

Celerybeat runs periodic tasks every 5 seconds regardless of , My environment: python 2.7.3 django 1.4.1 virtualenv django-celery 3.0.6 celery 3.0.6 { 'task': 'app.tasks.every_day', 'schedule': crontab(minute=30, hour=0), }, } Always make timezones aware even if UTC is disabled. django_celery_beat.models.PeriodicTask; This model defines a single periodic task to be run. It must be associated with a schedule, which defines how often the task should run. django_celery_beat.models.IntervalSchedule; A schedule that runs at a specific interval (e.g. every 5 seconds). django_celery_beat.models.CrontabSchedule

I would go with your alternative solution by scheduling another task to enable/disable the primary task:

# tasks.py
from djcelery.models import PeriodicTask

@app.task
def toggle_thirty_second_task:
    # you would use whatever you named your task below
    thirty_second_tasks = PeriodicTask.objects.filter(name='runs-every-30-seconds')
    if thirty_second_tasks:
        # this returned an array, so we'll want to look at the first object
        thirty_second_task = thirty_second_tasks[0]
        # toggle the enabled value
        thirty_second_task.enabled = not thirty_second_task.enabled
        thirty_second_task.save()

Then just schedule this task using a crontab for the hours that you need to toggle. Alternatively, you could have a task scheduled to turn it on and one to turn it off and not deal with the toggling logic.

Hope this helps.

sc

Run task every 20 seconds? · Issue #179 · Koed00/django-q · GitHub, Are there any external obstacles to using seconds instead of minutes as the code, it seems like 1 minute is the shortest possible interval between tasks. Smells of crontab for no reason frowning Personally I'm just resigned to using Celery, even on projects where it is an overkill of criminal proportions. Cron cannot run more than once a minute, while celery can (I have a task run every 90 seconds which checks an email queue to send messages, and another which cleans the online users list). A cron line has to call a script or a unique command, with absolute path and user info.

Periodic Tasks, For Django users the time zone specified in the TIME_ZONE setting will be used, or you Example: Run the tasks.add task every 30 seconds. from celery.​schedules import crontab CELERYBEAT_SCHEDULE = { # Executes crontab(​minute=0, hour='*/2,*/3'), Execute every even hour, and every hour divisible by three. I recently upgraded to celery 3 and have been experiencing some strange behavior with celerybeat, which was acting normally before the upgrade. My environment: python 2.7.3 django 1.4.1 virtualenv django-celery 3.0.6 celery 3.0.6 Regardl

Celerybeat Schedule tasks twice a day in different hours, Django celery crontab every 30 seconds - is it even possible? Is it possible to run the django celery crontab very 30 seconds DURING SPECIFIC HOURS? But it's easily extensible to do what you want, e.g. the django-celery scheduler is just a subclass reading and writing the schedule to the database (with some optimizations on top). Also you can use the django-celery scheduler even for non-Django projects. Something like this: Install django + django-celery: $ pip install -U django django-celery

celery cron - Search, celery cron Idaily is a place to get your questions answered. Ask questions and Django celery crontab every 30 seconds - is it even possible? Ask Question  Using a timedelta for the schedule means the task will be sent in 30 second intervals (the first task will be sent 30 seconds after celery beat starts, and then every 30 seconds after the last run). A crontab like schedule also exists, see the section on Crontab schedules.

Comments
  • I need it to run every 30 seconds DURING SPECIFIC HOURS. I too came across the very first example they have in the documentation.
  • am also looking for a solution to this (and came across this post). would be really nice if timedelta could be combined w/crontab.