脚本运行时执行函数

2024-04-19 17:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,这是个简单的问题,因为我不知道它叫什么。。你知道吗

假设我在python中有一个循环

if pattern in buffer:
        while logme == "y":
            logging.basicConfig(filename='hook.log',level=logging.DEBUG)
            logging.debug("Pre-Encrypted: %s" % buffer)
            print "Pre-Encrypted: %s" % buffer
        else:
            print "Pre-Encrypted: %s" % buffer

当我在循环运行时按键盘键(如p)并让它执行命令(如暂停循环、退出或执行任何操作)时,我如何才能做到这一点?不是命令行参数,而是在实际程序运行时。。你知道吗


Tags: inlogifloggingbufferhookfilenamelevel
2条回答

您可以使用curses,这会有点复杂。你知道吗

一个快速的解决方法是在Python中拦截SIGINT(Ctrl-C,KeyboardInterrupt)。你知道吗

def foo():
  try:
     long_running_process()
  catch KeyboardInterrupt:
     deal_with_interrupt()

除了违反了对Ctrl-C行为的期望之外,这也没有提供一个明显的方法来重启这个东西。你知道吗

相关问题 更多 >