Created
October 22, 2017 16:24
-
-
Save channeng/2903e45ad75583de421424abbfffb3c2 to your computer and use it in GitHub Desktop.
Celery Flask setup -- final app.py
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 flask import Flask | |
| from celery import Celery | |
| import celeryconfig | |
| app = Flask(__name__) | |
| app.config.from_object('config') | |
| def make_celery(app): | |
| # create context tasks in celery | |
| celery = Celery( | |
| app.import_name, | |
| broker=app.config['BROKER_URL'] | |
| ) | |
| celery.conf.update(app.config) | |
| celery.config_from_object(celeryconfig) | |
| 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