【发布时间】:2017-08-13 21:33:58
【问题描述】:
我有一个 python 程序需要几分钟才能完成。我有一些调试代码,仅在设置变量时打印。在我当前的实现中,该变量是通过命令行或环境变量设置的。我想在程序执行时动态启用/禁用调试。
例如,考虑以下代码:
import time
import os
debugging=False
if "DEBUG" in os.environ:
debugging = True
def debug():
if debugging:
print("debug statement");
def enable_debugging():
global debugging
debugging = True
def disable_debugging():
global debugging
debugging = False
print("1")
debug()
time.sleep(20)
print("2")
debug()
所以当程序在调试关闭的情况下执行时,如何在程序执行时动态启用调试?换句话说,当输入特定字符串时,如何执行函数enable_debugging(可能在单独的线程中)?
【问题讨论】:
-
做了一些实验,找到了解决办法。谢谢!
标签: python