Skip to content

Flask in Celery context #56

Description

@POD666

Hi,
I have tried to use enable_request_id_propagation with celery but during debugging found this:
image

I have a lot of things bound to my flask app (e.g. flask-sqlalchemy) so in celery, I just create an instance of my flask app to interact with DB and much more.

Since both flask and celery contexts are available, MultiContextRequestIdFetcher seems to handle it in the wrong way.
I guess this code should be changed to iterate until a first non-empty value is returned instead of returning on a first non-exception result:

class MultiContextRequestIdFetcher(object):
    ...

    def __call__(self):
        for ctx_fetcher in self.ctx_fetchers:
            try:
                return ctx_fetcher()
            except ExecutedOutsideContext:
                continue
        return None

For now, I fixed it by reversing handlers in current_request_id during celery worker startup:

enable_request_id_propagation(celery)
current_request_id.ctx_fetchers = current_request_id.ctx_fetchers[::-1]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions