Skip to content

Instantly share code, notes, and snippets.

@MikulasZelinka
Last active October 19, 2023 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikulasZelinka/c9c2031fb1bf6992a230332cd10ef120 to your computer and use it in GitHub Desktop.
Save MikulasZelinka/c9c2031fb1bf6992a230332cd10ef120 to your computer and use it in GitHub Desktop.
Celery Python – minimal example (app is started from Python, task is also sent from Python)

Execute in the following order:

docker run -d -p 6379:6379 redis
python celery_app.py
python send_task.py
import logging
from celery import Celery
logging.getLogger("celery").setLevel(logging.DEBUG)
app = Celery(
"myapp", # not sure if/how this matters
broker="redis://localhost", # to connect to the broker
backend="redis://localhost", # to be able to collect task results
include=["celery_app"], # so that the add task is registered
)
@app.task
def add(x, y):
return x + y
if __name__ == "__main__":
app.start(["worker"])
from celery_app import add
if __name__ == "__main__":
# Send a task to the Celery app.
result = add.delay(3, 5)
# Wait for the result of the task and print it.
print("Waiting for the task to complete...")
result_value = result.get()
print(f"Task result: {result_value}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment