从运行的python脚本中检测optimize标志是否为-O或-OO [英] Detect from a running python script if the optimize flag is -O or -OO
问题描述
有时候我想产生一个具有与启动父进程相同的优化标志的子进程.
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__
但是通过这种方式,我同时匹配了-O
和-OO
标志.
But this way I match both -O
and -OO
flags.
是否存在一些包含该信息的python内部状态?
Is there some python internal status that contains that info?
推荐答案
在对文档进行一些挖掘之后,我发现sys.flags
结构序列(
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
这篇关于从运行的python脚本中检测optimize标志是否为-O或-OO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!