【发布时间】:2017-12-01 19:34:00
【问题描述】:
我正在 Windows 上用 Python 3 编写一些代码,如下所示:
try:
do something that takes a long time
(training a neural network in TensorFlow, as it happens)
except KeyboardInterrupt:
print('^C')
print a summary of results
still useful even if the training was cut short early
如果使用python foo.py 直接从控制台运行,这将完美运行。
但是,如果对 Python 的调用在批处理文件中,它最终会执行上述所有操作,但仍会使用“终止批处理作业”提示向控制台发送垃圾邮件。
有没有办法阻止这种情况发生?通过在 Python 中完全吃掉 ^C,直接跳出批处理文件或其他方式?
【问题讨论】:
-
你无法通过 Python 控制它。连接到控制台主机 (conhost.exe) 的每个进程都会在会话服务器 (csrss.exe) 创建的新线程上接收控制台控制事件。 CMD 的处理程序设置一个标志。当它在 Python 退出后恢复其调度循环时,它会看到 Ctrl+C 标志已设置并打印“终止批处理作业”消息。
标签: python windows batch-file windows-console