last day (15 days later) » 

6:41 PM
1
A: Flask with create_app, SQLAlchemy and Celery

MaartenI was off with the current_app advice. Your celery object needs access to the application context. I found some information online about creating the Celery object with a factory function. Example below is tested without a message broker. #factory.py from celery import Celery from config import...

 
Hi Maarten, thanks for your answer. With your hint I got it working with a few minor adaptions. So I would like to accept it as correct answer but nonetheless post the full working answer. Do you know what is the usual approach?
 
Happy to hear you were able to fix your code. If you want to accept the answer you can post your working code in an edit in the original question.
 
Playing around a bit with my current solution and with your suggestion, I'm struggeling to understand why decorator needs to set explicitly the base. e.g. @celery.task(base=celery.Task) def do_some_stuff(): print g whereas just @celery.task does give an outside application context.
 
Did you use "celery.Task = ContextTask" in your factory function? Or maybe you put your working code somewhere and link to it?
 
yes, I did, see above.
 
6:41 PM
see my updated answer above. I removed the comments before the with statement and I put the print statement within the with statement. that allowed me to remove "base=celery.Task" (on a Linux mint machine)
 
The problem with that approach is that you then create the application context directly. Which means the whole ContextTask class is kind of useless.
 
Try removing you *.pyc files
I have your working code running now
Hi
And to retort, on a side note: the app.app context is created when celery.app is created, in the factory function :)
 
7:00 PM
And you are right about the Context task being redundant when using the with statement
 
Thanks, my current version is under:
this works now
in the celery factory I also added flask app
so that for the celery_prerun function I can use it to get the flask app context.
g.db didn't really work so I import it now directly from the extension
There is a few other clean ups.
Anyway thank for your help, if you find anything you would improve let me know.
 
I will. I got to try some new stuff while working with your question so thank you.
And the factory is exactly where you want to overwrite stuff so you can create it once and then just use it. it will share the config with your web app
 
agreed, I think the solution is ok now. I had looked at your links and tried different things, but I never really could get it to work
 

last day (15 days later) »