Created
November 16, 2016 20:01
-
-
Save HeyHugo/a9fb6912960e9dfec34805f6f9fd8ae5 to your computer and use it in GitHub Desktop.
Celery extension for flask
This file contains 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
import flask | |
from celery import Celery | |
class FlaskCelery(Celery): | |
def __init__(self, *args, **kwargs): | |
self.app = None | |
super(FlaskCelery, self).__init__(*args, **kwargs) | |
self.patch_task() | |
if 'app' in kwargs: | |
self.init_app(kwargs['app']) | |
def patch_task(self): | |
TaskBase = self.Task | |
_celery = self | |
class ContextTask(TaskBase): | |
abstract = True | |
def __call__(self, *args, **kwargs): | |
if flask.has_app_context(): | |
return TaskBase.__call__(self, *args, **kwargs) | |
else: | |
with _celery.app.app_context(): | |
return TaskBase.__call__(self, *args, **kwargs) | |
self.Task = ContextTask | |
def init_app(self, app): | |
self.app = app | |
self.config_from_object(app.config) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment