librelist archives

« back to archive

Celery, blueprints, and application factories

Celery, blueprints, and application factories

From:
Bing Xia
Date:
2013-12-16 @ 15:41
Hi,

I've been working on a site using Celery for background tasks, and I've
recently tried to refactor my code to use blueprints and application
factories. However, I seem to be running into a circular dependency issue
which I like some help with. The situation is as follows:

I want to create the app inside an application factory - the create_app
function. Inside this function, I try to register my blueprints. When I
import my blueprints, one of them imports tasks from tasks.py, which has to
create a celery instance, which needs the create_app function.

Code below:

factory.py:

from views import job  # This is the blueprint

def create_app():
    app = Flask()
    # Some configuration here
    app.register_blueprint(job.bp)
    return app

def create_celery_app(app=None):
    app = app or create_app()
    celery = Celery(broker=app.config['CELERY_BROKER_URL'])
    return celery

job.py:

from tasks import submit_job  # This is part of the circle

bp = Blueprint()

bp.route(...etc...)

tasks.py:

from factory import create_celery_app
celery = create_celery_app()

Basically, it seems like if I want my celery instance to get its config
from the Flask app, it means that my blueprints have to depend on the Flask
app. Any advice on resolving this issue would be greatly appreciated.

Thanks,
Bing

Re: [flask] Celery, blueprints, and application factories

From:
Matthias Urlichs
Date:
2013-12-16 @ 15:55
Hi,

Bing Xia:
> from views import job  # This is the blueprint
> 
> def create_app():
>     app = Flask()
>     # Some configuration here
>     app.register_blueprint(job.bp)
>     return app
> 
Why don't you import inside the function?

> def create_app():
>     from views import job  # This is the blueprint
>
>     app = Flask()
>     # Some configuration here
>     app.register_blueprint(job.bp)

Cycle broken.

-- 
-- Matthias Urlichs

Re: [flask] Celery, blueprints, and application factories

From:
Bing Xia
Date:
2013-12-16 @ 16:19
This doesn't seem to work either - the import triggers a call to
create_celery_app which calls create_app.
On Dec 16, 2013 10:58 AM, "Matthias Urlichs" <matthias@urlichs.de> wrote:

> Hi,
>
> Bing Xia:
> > from views import job  # This is the blueprint
> >
> > def create_app():
> >     app = Flask()
> >     # Some configuration here
> >     app.register_blueprint(job.bp)
> >     return app
> >
> Why don't you import inside the function?
>
> > def create_app():
> >     from views import job  # This is the blueprint
> >
> >     app = Flask()
> >     # Some configuration here
> >     app.register_blueprint(job.bp)
>
> Cycle broken.
>
> --
> -- Matthias Urlichs
>

Re: [flask] Celery, blueprints, and application factories

From:
Scott Werner
Date:
2013-12-16 @ 16:32
I would check out the setup using application factories, blueprints, and
celery tasks at

http://mattupstate.com/python/2013/06/26/how-i-structure-my-flask-applications.html
 and  https://github.com/mattupstate/overholt.


On Mon, Dec 16, 2013 at 11:19 AM, Bing Xia <aix.bing@gmail.com> wrote:

> This doesn't seem to work either - the import triggers a call to
> create_celery_app which calls create_app.
> On Dec 16, 2013 10:58 AM, "Matthias Urlichs" <matthias@urlichs.de> wrote:
>
>> Hi,
>>
>> Bing Xia:
>> > from views import job  # This is the blueprint
>> >
>> > def create_app():
>> >     app = Flask()
>> >     # Some configuration here
>> >     app.register_blueprint(job.bp)
>> >     return app
>> >
>> Why don't you import inside the function?
>>
>> > def create_app():
>> >     from views import job  # This is the blueprint
>> >
>> >     app = Flask()
>> >     # Some configuration here
>> >     app.register_blueprint(job.bp)
>>
>> Cycle broken.
>>
>> --
>> -- Matthias Urlichs
>>
>


-- 
Scott Werner
843-321-9598
scott.werner.vt@gmail.com

Re: [flask] Celery, blueprints, and application factories

From:
Bing Xia
Date:
2013-12-16 @ 17:05
Overholt is exactly what I looked at to base my redactor off of.
Unfortunately this circular dependency is present in overholt as well.
On Dec 16, 2013 11:35 AM, "Scott Werner" <scott.werner.vt@gmail.com> wrote:

> I would check out the setup using application factories, blueprints, and
> celery tasks at
> 
http://mattupstate.com/python/2013/06/26/how-i-structure-my-flask-applications.html
>  and  https://github.com/mattupstate/overholt.
>
>
> On Mon, Dec 16, 2013 at 11:19 AM, Bing Xia <aix.bing@gmail.com> wrote:
>
>> This doesn't seem to work either - the import triggers a call to
>> create_celery_app which calls create_app.
>> On Dec 16, 2013 10:58 AM, "Matthias Urlichs" <matthias@urlichs.de> wrote:
>>
>>> Hi,
>>>
>>> Bing Xia:
>>> > from views import job  # This is the blueprint
>>> >
>>> > def create_app():
>>> >     app = Flask()
>>> >     # Some configuration here
>>> >     app.register_blueprint(job.bp)
>>> >     return app
>>> >
>>> Why don't you import inside the function?
>>>
>>> > def create_app():
>>> >     from views import job  # This is the blueprint
>>> >
>>> >     app = Flask()
>>> >     # Some configuration here
>>> >     app.register_blueprint(job.bp)
>>>
>>> Cycle broken.
>>>
>>> --
>>> -- Matthias Urlichs
>>>
>>
>
>
> --
> Scott Werner
> 843-321-9598
> scott.werner.vt@gmail.com
>