Created
October 22, 2017 16:07
-
-
Save channeng/1d3005307455582e9172892f22ae007f to your computer and use it in GitHub Desktop.
Celery Flask setup - second step
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 | |
| 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) | |
| 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