Using celery with Flask app context gives Popped wrong app context. AssertionError

2024/4/14 11:34:40

I'm more or less using the setup to run Celery tasks using your flask app context from here:

I'm getting the same error message as Create, manage and kill background tasks in flask app

but, I'm getting it in the actual worker where the Celery task is being executed. Here is the trace:

worker_1 | Traceback (most recent call last):
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/celery/app/", line 240, in trace_task
worker_1 |     R = retval = fun(*args, **kwargs)
worker_1 |   File "/code/app/", line 42, in __call__
worker_1 |     return TaskBase.__call__(self, *args, **kwargs)
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/flask/", line 186, in __exit__
worker_1 |     self.pop(exc_value)
worker_1 |   File "/usr/local/lib/python2.7/dist-packages/flask/", line 178, in pop
worker_1 |     % (rv, self)
worker_1 | AssertionError: Popped wrong app context.  (<flask.ctx.AppContext object at 0x47a5790> instead of <flask.ctx.AppContext object at 0x427e390>)

Anyone have any ideas? Running in the app context should be fixing this problem, not creating it!



I know this is 4 years since you posted, but I was battling with a similar problem excep using rq not celery.

What everyone on the web tells you is that you need to add context to your app, eg as per the Supertutorial:

app = create_app()

What they don't tell you is that if you have anything else in this file that will cause your primary instance of the app to open it (Eg another function), then you'll hit the 2 lines above and get the error.

My solution was to put the 2 lines inside of the actual async function that I'm offloading to a worker like this:

def run_async_backup(machine_id):app = create_app()app.app_context().push()# your code for the worker...

This means the primary instance won't hit them - while the worker will (assume only the worker calls this function).

Related Q&A

How do I reduce the verbosity of chromedriver logs when running it under selenium?

My jenkins failure reports for my functional tests are full of lines like this:selenium.webdriver.remote.remote_connection: DEBUG: Finished Request selenium.webdriver.remote.remote_connection: DEBUG: P…

How to model python properties in UML diagram

What is a good practice to model Python properties in a UML class diagram? Properties themselves are class objects, their getter and setter are class functions. From Outside the class they look like i…

Linear regression with tensorflow

I trying to understand linear regression... here is script that I tried to understand: A linear regression learning algorithm example using TensorFlow library. Author: Aymeric Damien Project: https://g…

Are null bytes allowed in unicode strings in PostgreSQL via Python?

Are null bytes allowed in unicode strings?I dont ask about utf8, I mean the high level object representation of a unicode string.BackgroundWe store unicode strings containing null bytes via Python in …

Why the irrelevant code made a difference?

I am thinking to make a progress bar with python in terminal. First, I have to get the width(columns) of terminal window. In python 2.7, there is no standard library can do this on Windows. I know mayb…

What values to use for FastCGI maxrequests, maxspare, minspare, maxchildren?

Im running a Django app using FastCGI and lighttpd.Can somebody explain me what I should consider when deciding what value to use for maxrequests, maxspare, minspare, maxchildren?These options are not…

How to calculate the Silhouette Score for each cluster separately in python

You can easily extract the silhouette score with 1 line of code that averages the scores for all your clusters but how do you extract each of the intermediate scores from the scikit learn implementatio…

Can I tell python to put an existing figure in a new figure?

Creating a certain plot is a lot of work, so I would like to automate this by create a function f() that returns a figure.I would like to call this function so that I can put the result in a subplot. …

django-crispy-forms have field and button on same row

I am needing to have a bootstrap PrependedText field with a button on the same row. I can get it on the same row but it shows the button before the textbox and I want it after. What am I doing wrong an…

Uploading file in python flask

I am trying to incorporate uploading a basic text/csv file on my web app which runs flask to handle http requests. I tried to follow the baby example in flasks documentation running on localhost here. …