Celery: why do I need a broker for periodic tasks?

I have a stand-alone script that resets a page, initiates a connection to the database, and writes the database to it. I need it to run periodically after x hours. I can do this with a bash script, with pseudocode:

while true
do
  python scraper.py
  sleep 60*60*x
done

From what I read about message brokers, they are used to send β€œsignals” from one running program to another, for example, in principle, in HTTP. Just as I have a piece of code that receives an email id from a user, it sends a signal with an email id to another piece of code that will send an email.

I need celery to perform a periodic heroic task. I already have mongodb on a separate server. Why do I need to start another server for rabbitmq or redis just for this? Can I use celery without a broker?

+6
source share
3 answers

Celery's architecture is designed to scale and distribute tasks across multiple servers. For sites like yours, this may be redundant. A queue service is usually needed to maintain a list of tasks and signal the status of completed tasks.

, . - "" , Redis , RabbitMQ. Redis .

Advanced Python scheduler, Redis, .

, , , Cron pure Python .

+10

:

, . , , .

MongoDB . . MongoDB.

+2

Django ,

PyPI:

pip install django-background-tasks

INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'background_task',
    # ...
)

:

python manage.py makemigrations background_task
python manage.py migrate

:

from background_task import background
from django.contrib.auth.models import User

@background(schedule=60)
def notify_user(user_id):
    # lookup user by id and send them a message
    user = User.objects.get(pk=user_id)
    user.email_user('Here is a notification', 'You have been notified')

notify_user . , Task . , . , - JSON. , user_id, User.

notify_user 60 :

notify_user(user.id)

( ), :

notify_user(user.id, schedule=90) # 90 seconds from now
notify_user(user.id, schedule=timedelta(minutes=20)) # 20 minutes from now
notify_user(user.id, schedule=timezone.now()) # at a specific time

:

notify_user.now(user.id)   # launch a notify_user function and wait for it
notify_user = notify_user.now   # revert task function back to normal function. 

. :

 notify_user(user.id, verbose_name="Notify user", creator=user)
+1
source

All Articles