Created
February 22, 2018 11:11
-
-
Save jezeniel/fae7ecd49a96d539de2ffc93c20938e3 to your computer and use it in GitHub Desktop.
Celery Part
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 import Celery | |
from flask import Flask, jsonify, request, render_template | |
def make_celery(app): | |
celery = Celery(app.import_name, | |
broker=app.config['CELERY_BROKER_URL'], | |
backend=app.config['CELERY_RESULT_BACKEND']) | |
celery.conf.update(app.config) | |
TaskBase = celery.Task | |
class ContextTask(TaskBase): | |
abstract = True | |
def __call__(self, *args, **kwargs): | |
with app.app_context(): | |
return TaskBase.__call__(self, *args, **kwargs) | |
celery.Task = ContextTask | |
return celery | |
app = Flask(__name__) | |
app.config.update( | |
CELERY_BROKER_URL='redis://localhost:6379', | |
CELERY_RESULT_BACKEND='redis://localhost:6379' | |
) | |
celery = make_celery(app) | |
@celery.task() | |
def add(a, b): | |
return a + b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment