librelist archives

« back to archive

running in gunicorn vs running with default server

running in gunicorn vs running with default server

From:
Lowell Kirsh
Date:
2012-05-09 @ 23:15
I have an app.py which has a main method (i.e. code inside 'if __name__ ==
'__main__':). I just realized that when running through gunicorn this code
does not run. I am wondering how to restructure my file so that code can
run? Here is some relevant stuff from my app.py:

class ProductionConfig(object):
    DEBUG = True
    KEYS = { ...}

class DevelopmentConfig(object):
    DEBUG = True
    KEYS = {...}

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    debug = os.environ.get('DEBUG', False)

    if debug:
        config = DevelopmentConfig
    else:
        config = ProductionConfig

    app.config.from_object(config)
    app.run(host='0.0.0.0', port=port)


In short, I'd like to configure my app cleanly. Is it ok to put most of the
config code outside of its block? Maybe just leave the app.run(...) as the
only line in that block?

Re: [flask] running in gunicorn vs running with default server

From:
Steven Kryskalla
Date:
2012-05-10 @ 03:17
On Wed, May 9, 2012 at 4:15 PM, Lowell Kirsh <lowellk@gmail.com> wrote:
> In short, I'd like to configure my app cleanly. Is it ok to put most of the
> config code outside of its block? Maybe just leave the app.run(...) as the
> only line in that block?

I would split it up like this:

class ProductionConfig(object):
    DEBUG = True
    KEYS = { ...}

class DevelopmentConfig(object):
    DEBUG = True
    KEYS = {...}

debug = os.environ.get('DEBUG', False)
if debug:
    config = DevelopmentConfig
else:
    config = ProductionConfig

app.config.from_object(config)

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

You can leave the "port" line inside the "if __name__" block because
it's only used for the development server, gunicorn doesn't use it.

Also, you probably don't want DEBUG to be on for your production configs.

-Steve

Re: [flask] running in gunicorn vs running with default server

From:
Lowell Kirsh
Date:
2012-05-10 @ 15:16
Thanks. Seems like it's working well now.

On Wed, May 9, 2012 at 8:17 PM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Wed, May 9, 2012 at 4:15 PM, Lowell Kirsh <lowellk@gmail.com> wrote:
> > In short, I'd like to configure my app cleanly. Is it ok to put most of
> the
> > config code outside of its block? Maybe just leave the app.run(...) as
> the
> > only line in that block?
>
> I would split it up like this:
>
> class ProductionConfig(object):
>    DEBUG = True
>    KEYS = { ...}
>
> class DevelopmentConfig(object):
>    DEBUG = True
>    KEYS = {...}
>
> debug = os.environ.get('DEBUG', False)
> if debug:
>    config = DevelopmentConfig
> else:
>    config = ProductionConfig
>
> app.config.from_object(config)
>
> if __name__ == '__main__':
>    port = int(os.environ.get('PORT', 5000))
>     app.run(host='0.0.0.0', port=port)
>
> You can leave the "port" line inside the "if __name__" block because
> it's only used for the development server, gunicorn doesn't use it.
>
> Also, you probably don't want DEBUG to be on for your production configs.
>
> -Steve
>



-- 
http://lowellkirsh.com