Here first I will tell you what is Celery?

Celery is an asynchronous task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well.” For this post, we will focus on the scheduling feature to periodically run a job/task.”

Why I needed it?

I wanted to send mass emails each day therefore I was looking for library in Django So I found it very nice and easy to implement.

In order to import Celery please use following steps:-

  1. Install Celery

2. Install Redis

3. Run Redis

3. Add follwoing lines in your settings.py

 

4. Create a celery.py file in your app and and paste below lines.

e.g -> celery_new/celery_new/celery.py(Here my project name is “celery_new”)

 

Note – Please make sure rename celery_new(it’s my project name) with your project name

5. Now, Add following lines in init.py under your app(e.g celery_new/celery_new/__init__.py)

 

6. Create tasks.py file under your app (e.g celery_new/celery_new/tasks.py) and paste following lines:-

 

7. Run following commands in your terminals

Now you should be able to see following output for —

celery -A celery_new worker -l info

— — — — — — — celery@IMCHLT071 v3.1.18 (Cipater)
— — **** — — —
— — * *** * — Linux-4.4.0–145-generic-x86_64-with-Ubuntu-16.04-xenial
— * — **** — —
– ** — — — — — [config]
– ** — — — — — .> app: celery_new:0x7fcdef6098d0
– ** — — — — — .> transport: redis://localhost:6379//
– ** — — — — — .> results: redis://localhost:6379
– *** — — * — — .> concurrency: 4 (prefork)
— ******* — —
— — ***** — — — [queues]
— — — — — — — .> celery exchange=celery(direct) key=celery

[tasks]
. celery_new.celery.debug_task
. sending_mails_to_users

[2019–05–11 17:44:16,005: INFO/MainProcess] Connected to redis://localhost:6379//
[2019–05–11 17:44:16,016: INFO/MainProcess] mingle: searching for neighbors
[2019–05–11 17:44:17,022: INFO/MainProcess] mingle: all alone
[2019–05–11 17:44:17,035: WARNING/MainProcess] /home/sandeepsingh/Django/celery_new/celery_venv/lib/python3.5/site-packages/celery/fixups/django.py:265: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!

[2019–05–11 17:44:17,036: WARNING/MainProcess] celery@IMCHLT071 ready.
[2019–05–11 17:45:00,032: INFO/MainProcess] Received task: sending_mails_to_users[c32cd28a-c0fe-410a-9d36–30ed14e13bc5]
[2019–05–11 17:45:00,033: INFO/MainProcess] Task sending_mails_to_users[c32cd28a-c0fe-410a-9d36–30ed14e13bc5] succeeded in 0.0005354700006137136s: None
[2019–05–11 17:46:00,144: INFO/MainProcess] Received task: sending_mails_to_users[68c32c6e-1c3e-4330–9807–6a00cd04bd00]

and for- celery -A celery_new beat -l info

Configuration ->
. broker -> redis://localhost:6379//
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%INFO
. maxinterval -> now (0s)
[2019–05–11 17:44:21,164: INFO/MainProcess] beat: Starting…
[2019–05–11 17:45:00,028: INFO/MainProcess] Scheduler: Sending due task sending_mails_to_users (sending_mails_to_users)
[2019–05–11 17:46:00,141: INFO/MainProcess] Scheduler: Sending due task sending_mails_to_users (sending_mails_to_users)
[2019–05–11 17:47:00,047: INFO/MainProcess] Scheduler: Sending due task sending_mails_to_users (sending_mails_to_users)