1👍
Well, as you haven’t provided any code, I will just try to write how I did the similar task you written about. My project structure was like this:
project
-settings.py
-manage.py
-app
-__init__py
-celery.py
-tasks.py
-views.py
-models.py
I configured celery in settings.py
like this:
from __future__ import absolute_import
BROKER_URL = 'pyamqp://guest:guest@wlocalhost:5672//' #read docs
CELERY_IMPORTS = ('app.tasks', )
from celery.schedules import crontab
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'schedule-name': {
'task': 'app.tasks', # example: 'files.tasks.cleanup'
'schedule': timedelta(seconds=30),
},
}
Code of celery.py
in app directory:
from __future__ import absolute_import
import os
from celery import Celery
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
app = Celery('blackwidow.communication.email_sending_method')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
app.conf.update(
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
And updated the __init__.py
file within that directory:–
from __future__ import absolute_import
from celery import app as celery_app
Code of the tasks.py
in app
directory:
from __future__ import absolute_import
import datetime
from celery.task.base import periodic_task
from django.core.mail import send_mail
@periodic_task(run_every=datetime.timedelta(seconds=30))
def email_sending_method():
send_mail('subject', 'body', 'from_me@admin.com' , ['to_me@admin.com'], fail_silently=False)
I added respective credentials/configurations for sending mail in settings.py
, and then ran this piece of code in command prompt:–
celery -A app worker -B -l info #I think you missed this.
And that should do the trick, it will send mail after every 30 seconds.
Source:stackexchange.com