Detect from a running python script if the optimize flag is -O or -OO

霸气de小男生 提交于 2019-12-08 15:40:04

问题


Sometime I'd like to spawn a child process with the same optimization flags used to start the parent.

I can use something like:

optimize = not __debug__

But this way I match both -O and -OO flags.

Is there some python internal status that contains that info?


回答1:


After some digging in the documentation I've found that the sys.flags struct sequence (http://docs.python.org/dev/library/sys#sys.flags) that has an optimize attribute containing the information I was searching for.

python -c "import sys; print sys.flags.optimize" -> 0

python -O -c "import sys; print sys.flags.optimize" -> 1

python -OO -c "import sys; print sys.flags.optimize" -> 2



来源:https://stackoverflow.com/questions/17317850/detect-from-a-running-python-script-if-the-optimize-flag-is-o-or-oo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!