Skip to content

Instantly share code, notes, and snippets.

@bryaneaton
Created May 31, 2022 17:43
Show Gist options
  • Save bryaneaton/cba0a9562c6813ab70fd9d1056fdb222 to your computer and use it in GitHub Desktop.
Save bryaneaton/cba0a9562c6813ab70fd9d1056fdb222 to your computer and use it in GitHub Desktop.
Celery Task Example, using Flask.
#!/usr/bin/env python3
from flask import Flask, jsonify
from celery import Celery
app = Flask(__name__)
app.config["CELERY_BROKER_URL"] = "redis://redis:6379"
celery = Celery(app.name, broker=app.config["CELERY_BROKER_URL"])
celery.conf.update(app.config)
@app.route('/')
def add_task():
""" A job is created in the celery queue, and executed when resources are available."""
for i in range(10000):
add.delay(i, i)
return jsonify({'status': 'ok'})
@celery.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