What is the purpose of ContextTask in celery in flask?

Viewed 9

Why do we need this code? What is the reason to use ContextTask?

If we integrate flask + celery does it mean that it can share objects in memory, or each and every gunicorn and celery worker will have its own processes and it's own memory?

class FlaskCelery(Celery):

    def __init__(self, *args, **kwargs):
        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)

celery = FlaskCelery()

can you recommend some good guides on how to properly integrate flask with celery?

0 Answers
Related