Skip to content

Instantly share code, notes, and snippets.

@channeng
Created October 22, 2017 16:00
Show Gist options
  • Select an option

  • Save channeng/cec3afc727c8a4c145d8b856e104f815 to your computer and use it in GitHub Desktop.

Select an option

Save channeng/cec3afc727c8a4c145d8b856e104f815 to your computer and use it in GitHub Desktop.
Celery Flask setup - first step
from os import environ
from flask import Flask
from celery import Celery
app = Flask(__name__)
# Configs
REDIS_HOST = "0.0.0.0"
REDIS_PORT = 6379
BROKER_URL = environ.get('REDIS_URL', "redis://{host}:{port}/0".format(
host=REDIS_HOST, port=str(REDIS_PORT)))
CELERY_RESULT_BACKEND = BROKER_URL
def make_celery(app):
# create context tasks in celery
celery = Celery(
app.import_name,
broker=BROKER_URL
)
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
celery = make_celery(app)
@app.route('/')
def view():
return "Hello, Flask is up and running!"
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment