Skip to content

Instantly share code, notes, and snippets.

@itsjef
Last active August 1, 2022 10:49
Show Gist options
  • Save itsjef/f040516fb154984dc15e57e4eb74b04a to your computer and use it in GitHub Desktop.
Save itsjef/f040516fb154984dc15e57e4eb74b04a to your computer and use it in GitHub Desktop.
Simple demo of how separate Celery workers can communicated with each other

Start RabbitMQ with Docker:

$ docker run --rm -d -p 5672:5672 rabbitmq:3

Then, in 3 different shells, run:

$ celery -A receiver worker -Q in -n receiver -l INFO
$ celery -A sender worker -Q out -n sender -l INFO
$ python sender.py 1 2 3 4
from celery import Celery
app = Celery(__name__, broker="amqp://localhost")
@app.task
def calculate_result(x, y):
result = x+y
app.send_task(
"sender.print_result",
args=(result,),
queue="out"
)
import sys
from celery import Celery
app = Celery(__name__, broker="amqp://localhost")
@app.task
def print_result(v):
print(v)
if __name__ == "__main__":
x, y, *_ = sys.argv[1:]
app.send_task(
"receiver.calculate_result",
args=(float(x), float(y)),
queue="in"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment