Skip to content

Instantly share code, notes, and snippets.

@ankurdhuriya
Created March 26, 2023 16:07
Show Gist options
  • Save ankurdhuriya/12ab94ff00d08d848ccb298c0f4bc112 to your computer and use it in GitHub Desktop.
Save ankurdhuriya/12ab94ff00d08d848ccb298c0f4bc112 to your computer and use it in GitHub Desktop.
Celery Concurrency Example
"""
In this example, we're configuring Celery to use threads for concurrency.
We've set the task_concurrency option to 4, which means that each worker
can handle up to 4 tasks simultaneously. We've also set the worker_prefetch_multiplier
option to 1, which means that each worker will only prefetch one task at a time.
"""
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//')
# Configure Celery to use threads for concurrency
app.conf.update(
task_concurrency=4, # Use 4 threads for concurrency
worker_prefetch_multiplier=1 # Prefetch one task at a time
)
@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