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.

有帮助吗?

解决方案

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.

其他提示

This works, though it is not particularly elegant.

python -O `which gunicorn` 'module.wsgi:application'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top