5

By optimized mode I mean no asserts, possibly no doc strings, .pyo rather then .pyc.

In short I have a django project runnning through gunicorn(v18.0) in the standard style (gunicorn 'module.wsgi:application')

I have been unable to find a reference in the docs or elsewhere online.

Aaron Schif
  • 2,421
  • 3
  • 17
  • 29
  • The maintainer [recommends](https://github.com/benoitc/gunicorn/issues/592#issuecomment-30316178) using [environment variables](https://docs.python.org/3/using/cmdline.html#environment-variables) for what would otherwise be command line options. – Kevin Dec 16 '21 at 20:42

2 Answers2

4

You can set the PYTHONOPTIMIZE environment variable if you really understand what you are doing.

# e.g.
# same as -O
export PYTHONOPTIMIZE=1
# same as -OO
export PYTHONOPTIMIZE=2

Reference: Python doc: Command line and environment

PYTHONOPTIMIZE

If this is set to a non-empty string it is equivalent to specifying the -O option. If set to an integer, it is equivalent to specifying -O multiple times.

But normally you should never do it !!!

Deestan's answer for another SO question "Best practice for Python Assert" is really great:

Asserts should be used to test conditions that should never happen.

The purpose is to crash early in the case of a corrupt program state.

Usually a django application uses many other libraries. When something critical happens, and those libraries believe that the application should crash immediately, they follow the best practice above and use asserts. You don't want to break that.

Community
  • 1
  • 1
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • 4
    `But normally you should never do it !!!` I disagree. `assert` statements are not meant for production exactly because they can be disabled by something out of your control. You should not rely on it in a production environment. See this answer from the same question https://stackoverflow.com/a/1838411/1272513 – alfetopito Jul 30 '19 at 15:33
0

This works, though it is not particularly elegant.

python -O `which gunicorn` 'module.wsgi:application'
Aaron Schif
  • 2,421
  • 3
  • 17
  • 29