Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active August 7, 2021 18:30
Show Gist options
  • Save thuwarakeshm/d2bc6cb26d6de3881e6e74ae377c6355 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/d2bc6cb26d6de3881e6e74ae377c6355 to your computer and use it in GitHub Desktop.
Massive Computation
from celery import Celery
from flask import Flask
import time
# 1. A function to create Celery object.
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config["CELERY_RESULT_BACKEND"],
broker=app.config["CELERY_BROKER_URL"],
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
# 2. Create a Flask app
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL="redis://localhost:6379",
CELERY_RESULT_BACKEND="redis://localhost:6379",
)
# 3. Connect Flask with Celery
celery = make_celery(app)
# 4. Now, we can create Celery tasks by annotating @celery.task
@celery.task()
def massive_computation(name):
time.sleep(5)
return f"Hello {name}"
# Create a redis database instance
# ------------------------------------------------------------------
import redis
r = redis.Redis(host="localhost", port=6379, db=0)
# -------------------------------------------------------------------------
r.set("last_calculated_value", 0) # Set it's initial value to 0.
# Asynchronous execution
# -------------------------------------------------------------------------
def fib(n):
"""This function will calculate the Fibonacci number"""
return n if n < 2 else fib(n - 1) + fib(n - 2)
@celery.task()
def massive_computation(num: int):
r.set("last_calculated_value", fib(num))
# -------------------------------------------------------------------------
# Web api endpoints
# -------------------------------------------------------------------------
@app.route("/set/<num>")
def set_fib(num: int = 0):
massive_computation.delay(int(num))
return "New Fibonacci number will be assigned soon."
@app.route("/current")
def get_current():
last_calculated_value = r.get("last_calculated_value")
return f"Current fibonacci number is {last_calculated_value}\n"
# -------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment