I've read several pages of Google results multiple times and I'm very confused about how to layout my project. I have managed to get Celerybeat working using the periodic_task decorator, but that is depreciated and is being removed. From what I understand, the docs suggest that CELERYBEAT_SCHEDULE is the replacement. I've created a file exactly as shown, but haven't yet figured out:
I find it very hard to find the right way to use Celery in a non-Django project.
It's a bit confusing that the docs make it look like your CELERYBEAT_SCHEDULE is a separate file. In reality, it's an entry in your app (aka: Celery() instance) config as you can see it listed here. So however you get the configuration into your app, that's where it goes.
There are many ways to get the configuration into the app. If you believe "Explicit is better than implicit", then you probably want to:
celeryconfig.py
import celeryconfig
app.config_from_object(celeryconfig)
Example celeryconfig.py file
from datetime import timedelta
BROKER_URL = "redis://redis.local:6379/0"
BROKER_TRANSPORT_OPTIONS = {'fanout_prefix': True, 'fanout_patterns': True, 'visibility_timeout': 480}
CELERY_RESULT_BACKEND = BROKER_URL
CELERYBEAT_SCHEDULE = {
'addrandom-to-16K-every-2-seconds': {
'task': 'celery_test.tasks.addrandom', # notice that the complete name is needed
'schedule': timedelta(seconds=2),
'args': (16000, 42)
},
}
CELERY_TIMEZONE = 'UTC'
Trying to put it into a file like beatschedule.py
and then running celery -A beatschedule beat
will get you AttributeError: 'module' object has no attribute 'celery'
I created this project to demo Celerybeat in action as described in the docs. I believe it does a better job of showing how it all works than reading the docs alone. I also created this Docker image to make getting it up and running as simple as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With