Skip to content

Instantly share code, notes, and snippets.

@ankurdhuriya
Created March 26, 2023 16:13
Show Gist options
  • Save ankurdhuriya/ba178c8d1d71ca93730ef94ecec35f99 to your computer and use it in GitHub Desktop.
Save ankurdhuriya/ba178c8d1d71ca93730ef94ecec35f99 to your computer and use it in GitHub Desktop.
Celery Heartbeats Example
"""
In this example, we're configuring Celery to use heartbeats.
We've set the worker_heartbeat option to 120 seconds, which means
that each worker will send a heartbeat message to the broker every 2 minutes.
This can help to ensure that tasks are not lost if a worker goes offline or crashes.
"""
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
# Configure Celery to use heartbeats
app.conf.update(
worker_heartbeat=120, # Send a heartbeat every 120 seconds
)
@app.task
def add(x, y):
return x + y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment