Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running gunicorn in python optimized mode

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.

like image 934
Aaron Schif Avatar asked Oct 01 '22 07:10

Aaron Schif


1 Answers

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.

like image 62
Leonardo.Z Avatar answered Oct 13 '22 09:10

Leonardo.Z