Last active
July 11, 2022 01:13
-
-
Save bennett39/3ce543e913fbe2f96fb633b2073ebebe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery_tutorial.celery import app | |
from .models import Calculation | |
def fib(n): | |
"""Calculate the Nth fibonacci number""" | |
if n < 0: | |
raise ValueError('Negative numbers are not supported') | |
elif n == 0: | |
return 0 | |
elif n <= 2: | |
return 1 | |
return fib(n - 2) + fib(n - 1) | |
@app.task(bind=True) | |
def fibonacci_task(self, calculation_id): | |
"""Perform a calculation & update the status""" | |
calculation = Calculation.objects.get(id=calculation_id) | |
try: | |
calculation.output = fib(calculation.input) | |
calculation.status = Calculation.STATUS_SUCCESS | |
except Exception as e: | |
calculation.status = Calculation.STATUS_ERROR | |
calculation.message = str(e)[:110] | |
calculation.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment