celery:
https://en.wikipedia.org/wiki/Celery_(software)#:~:text=Celery%20is%20written%20in%20Python,js%20client.
Celery is written in Python, but the protocol can be implemented in any language. It can also operate with other languages using webhooks. There is also a Ruby-Client called RCelery, a PHP client, a Go client, a Rust client, and a Node. js client.
cleery beat vs celery worker
https://stackoverflow.com/questions/19679191/whats-difference-between-celeryd-celery-worker-celerybeat
celerybeat
is a scheduler that sends predefined tasks to a celery worker
at a given time. You only need to bother with this if you want to run a task on a schedule. For example, if you had a task called backup-database that needed to be run every day at 1am, you could add that to the CELERYBEAT_SCHEDULE
in your conf, which would look something like this.
CELERYBEAT_SCHEDULE = {
'backup-database': {
'task': 'tasks.backup_database',
'schedule': crontab(hour=1, minute=0, day_of_week='*'),
'args': (16, 16)
},
}
celery, django, redis:
https://realpython.com/asynchronous-tasks-with-django-and-celery/
- Producer: Your Django app
- Message Broker: The Redis server
- Consumer: Your Celery app
When starting Celery with this command, you provide the name of the module that contains your Celery app instance,
"django_celery"
, to-A
.for docker-compose -A name_of_folder_contains_celery.py
To include celery in django :
https://realpython.com/asynchronous-tasks-with-django-and-celery/
django_celery/ ├── __init__.py ├── asgi.py ├── celery.py ├── settings.py ├── urls.py └── wsgi.py
Celery recommends using this module to define the Celery application instance. Open the file in your favorite text editor or IDE and add the necessary code:
1# django_celery/celery.py 2 3import os 4from celery import Celery 5 6os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings") 7app = Celery("django_celery") 8app.config_from_object("django.conf:settings", namespace="CELERY") 9app.autodiscover_tasks()
# django_celery/settings.py # ... NOTE by default redis runs on port 6379
# Celery settings CELERY_BROKER_URL = "redis://localhost:6379" CELERY_RESULT_BACKEND = "redis://localhost:6379"
These two entries give your Celery application instance enough information to know where to send messages and where to record the results.
django_celery/ ├── __init__.py ├── asgi.py ├── celery.py ├── settings.py ├── urls.py └── wsgi.py
Open the file in your text editor. In a default Django project, each app folder has an
__init__.py
file which helps to mark it as a module. The file is empty by default, but you can add code to influence the import behavior.To make sure that your Celery app is loaded when you start Django, you should add it to
__all__
:# django_celery/__init__.py from .celery import app as celery_app __all__ = ("celery_app",)
To start celery, not celery is a module in python,(venv) $ python -m celery -A django_celery worker
For eveything, including redis, celery, django to be wrapped in docker-compose
current example:
https://testdriven.io/blog/django-celery-periodic-tasks/
further example:
https://soshace.com/dockerizing-django-with-postgres-redis-and-celery/
note: since celery is written in python and need python to start, it can use the
same dockerFile as django, and map the same volume so it knows where to find the folder that contains celery.py, the following example contains celery.py in core/celery.py
version: '3.8' services: web: build: ./project command: python manage.py runserver 0.0.0.0:8000 volumes: - ./project/:/usr/src/app/ ports: - 1337:8000 environment: - DEBUG=1 - SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] depends_on: - redis redis: image: redis:alpine celery: build: ./project command: celery -A core worker -l info volumes: - ./project/:/usr/src/app/ environment: - DEBUG=1 - SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] depends_on: - redis celery-beat: build: ./project command: celery -A core beat -l info volumes: - ./project/:/usr/src/app/ environment: - DEBUG=1 - SECRET_KEY=dbaa1_i7%*3r9-=z-+_mz4r-!qeed@(-a_r(g@k8jo8y3r27%m - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] depends_on: - redis
Note: by default redis runs on port 6379, and celery is a module in python which can bestarted using $ python -m celery -A django_celery worker
No comments:
Post a Comment