librelist archives

« back to archive

Execute at flask initialization

Execute at flask initialization

From:
Martin Stiborský
Date:
2012-06-07 @ 20:57
Hello,
I'd like to execute some method at time when Flask application is initialized.
It is a method for test if all needed things are ready to run the
application without problems.
If the some problem occured, the application should fail shut down
with explaining error message.

I know how to do it if the application is started "as module" directly
from shell, then I can easily use if __name__ == "__main__" block, but
how if the application is started with uwsgi for example?

Is something like that possible with Flask?

-- 
S pozdravem
Martin Stiborský

Jabber: stibi@njs.netlab.cz

Re: [flask] Execute at flask initialization

From:
Sean Lynch
Date:
2012-06-08 @ 01:47
Would registering with @app.before_first_request occur soon enough for you
to accomplish your checks?

http://flask.pocoo.org/docs/api/#flask.Flask.before_first_request

On Thu, Jun 7, 2012 at 4:57 PM, Martin Stiborský <martin.stiborsky@gmail.com
> wrote:

> Hello,
> I'd like to execute some method at time when Flask application is
> initialized.
> It is a method for test if all needed things are ready to run the
> application without problems.
> If the some problem occured, the application should fail shut down
> with explaining error message.
>
> I know how to do it if the application is started "as module" directly
> from shell, then I can easily use if __name__ == "__main__" block, but
> how if the application is started with uwsgi for example?
>
> Is something like that possible with Flask?
>
> --
> S pozdravem
> Martin Stiborský
>
> Jabber: stibi@njs.netlab.cz
>

Re: [flask] Execute at flask initialization

From:
David
Date:
2012-06-08 @ 02:10
Pardon my ignorance

but couldnt you just call a function like

def create_app():
     app = Flask(__name__)
     def somefunc():
          some app startup stuff

     somefunc()

     return app

app = create_app()

and wouldn't this run only once when the app starts up?

Anyone object to this idea...


On 6/7/12 9:47 PM, Sean Lynch wrote:
> Would registering with @app.before_first_request occur soon enough for 
> you to accomplish your checks?
>
> http://flask.pocoo.org/docs/api/#flask.Flask.before_first_request
>
> On Thu, Jun 7, 2012 at 4:57 PM, Martin Stiborský 
> <martin.stiborsky@gmail.com <mailto:martin.stiborsky@gmail.com>> wrote:
>
>     Hello,
>     I'd like to execute some method at time when Flask application is
>     initialized.
>     It is a method for test if all needed things are ready to run the
>     application without problems.
>     If the some problem occured, the application should fail shut down
>     with explaining error message.
>
>     I know how to do it if the application is started "as module" directly
>     from shell, then I can easily use if __name__ == "__main__" block, but
>     how if the application is started with uwsgi for example?
>
>     Is something like that possible with Flask?
>
>     --
>     S pozdravem
>     Martin Stiborský
>
>     Jabber: stibi@njs.netlab.cz <mailto:stibi@njs.netlab.cz>
>
>

Re: [flask] Execute at flask initialization

From:
Martin Stiborský
Date:
2012-06-08 @ 07:15
Sean: yep, I tried before_first_request, but this is not soon enough :(

David: This is it, now when I see it, I feel again like stupid, it
can't be more obvious :D Thanks!



-- 
S pozdravem
Martin Stiborský

Jabber: stibi@njs.netlab.cz

Re: [flask] Execute at flask initialization

From:
David Shawley
Date:
2012-06-12 @ 10:35
On Thu, Jun 7, 2012 at 4:57 PM, Martin Stiborský
<martin.stiborsky@gmail.com> wrote:
> Hello,
> I'd like to execute some method at time when Flask application is initialized.
> It is a method for test if all needed things are ready to run the
> application without problems.
> If the some problem occured, the application should fail shut down
> with explaining error message.
>
> I know how to do it if the application is started "as module" directly
> from shell, then I can easily use if __name__ == "__main__" block, but
> how if the application is started with uwsgi for example?
>
> Is something like that possible with Flask?
>

I would recommend use a subclass of flask.Flask.  You can perform your
initial checks in the constructor.

    class FlaskApp(flask.Flask):
        def __init__(self, *args, **rest):
            super(FlaskApp, self).__init__(__package__, *args, **rest)
            # insert startup code here

    if __name__ == '__main__':
        app = FlaskApp()
        app.run()

I almost always subclass flask.Flask to add functionality to it.
-- 
People think right angles produce harmony, but they don't. They
produce sleep. -- James Krenov