librelist archives

« back to archive

Detecting debug mode

Detecting debug mode

From:
Date:
2013-06-19 @ 19:05
Hi,

I've got an app that I either run in debug mode or served by nginx via 
uwsgi. I want to choose a configuration to load depending on whether I am 
in debug mode or uwsgi. What's the best way to detect this? Debug is set 
during "app.run", but ideally I'd like to load the config file before 
this. Also, "app.run" enters the run loop, so the path of execution is now
elsewhere. Is there a hook where code can be run right after "app.run"? 
Ideally, I'd like to perform this check without having uwsgi installed on 
the (development) machine.

Cheers,
Demitri

Re: [flask] Detecting debug mode

From:
dorian i
Date:
2013-06-19 @ 19:20
Since uwsgi is not installed in development box, what about this:

try:
    import uwsgi
    print 'production'
except ImportError:
    print 'develmode'



On Wed, Jun 19, 2013 at 9:05 PM, <thatsanicehatyouhave@me.com> wrote:

> Hi,
>
> I've got an app that I either run in debug mode or served by nginx via
> uwsgi. I want to choose a configuration to load depending on whether I am
> in debug mode or uwsgi. What's the best way to detect this? Debug is set
> during "app.run", but ideally I'd like to load the config file before this.
> Also, "app.run" enters the run loop, so the path of execution is now
> elsewhere. Is there a hook where code can be run right after "app.run"?
> Ideally, I'd like to perform this check without having uwsgi installed on
> the (development) machine.
>
> Cheers,
> Demitri
>

Re: [flask] Detecting debug mode

From:
Date:
2013-06-19 @ 20:07
On Jun 19, 2013, at 3:20 PM, dorian i <dorian.hoxha@gmail.com> wrote:

> Since uwsgi is not installed in development box, what about this:

Thanks. Though, I might install it (or someone else); it doesn't seem like
the most robust solution. :\

Re: [flask] Detecting debug mode

From:
Steven Kryskalla
Date:
2013-06-19 @ 20:31
On Wed, Jun 19, 2013 at 12:05 PM,  <thatsanicehatyouhave@me.com> wrote:
> I've got an app that I either run in debug mode or served by nginx via 
uwsgi. I want to choose a configuration to load depending on whether I am 
in debug mode or uwsgi. What's the best way to detect this? Debug is set 
during "app.run", but ideally I'd like to load the config file before 
this. Also, "app.run" enters the run loop, so the path of execution is now
elsewhere.

You control the code that calls app.run or returns the app for uwsgi
to serve up, correct? I don't think you need a hook, you should have
everything you need already to do this. For example:

1. Set an environment variable (DEBUG=1 vs. DEBUG=0, or
APP_ENV=development vs. APP_ENV=production) and detect this
environment variable using os.environ before you load the configs and
call app.run.
2. Have a single config file, but with different contents on your
development machine vs. your production server (e.g. keep a
config.example.py inside the repo, which you rename to config.py to
get started in development, and a separate production config.py is
managed outside of your app's repo). Your app will always look in the
same spot for the config file, but the contents will be different
depending on what config file has been deployed.
3. Write a script that starts your app, use command line params to
specify which config to use, or whether to use debug mode (e.g. run.py
--config=debug.cfg, run.py --debug, etc.)
4. Look at other things in the environment, the hostname or user name
or the path the app is running from, and use this to decide which
configs to use.
5. Etc.. there are tons of ways to do this. Give more info about your
setup & your code if you want more specific advice.

-Steve

Re: [flask] Detecting debug mode

From:
Date:
2013-06-20 @ 20:32
Hi Steve, Dorian,

Thanks for the many suggestions. I guess I was looking for an automatic 
way to see if the app was running under uWSGI. I do have a script that 
starts the app and implemented a command line "--debug" parameter to do 
the job.

After a bit more reading, I discovered that dorian's suggestion was 
actually a very good one. If you try "import uwsgi" from an app not 
launched/served by uWSGI, then the import will fail with an ImportError, 
and here's the important bit, *even when uwsgi is installed on the 
system*. Slightly non-standard behavior than I expected, but good to know!

Thanks!

Demitri