停止 python 进程,以便上下文管理器仍然调用 __exit__ (在 Windows 中)

Bat*_*730 6 python windows

如何以任何活动的上下文管理器在关闭之前优雅地调用它们的 __exit__ 函数的方式停止 python 进程?

我使用上下文管理器(__enter__() 和 __exit__())来可靠、安全地关闭与光学硬件的连接。这一直很有效,尽管我们现在开始执行运行数小时的例程。通常我们会在启动一个程序后不久意识到我们有一个错误,并且宁愿停止这个过程。

我一直在运行来自 PyCharm 的代码,它允许您“停止”正在运行的进程。无论我是在调试还是在运行,这似乎都会立即终止进程。__exit__ 函数似乎没有被调用。

此外,控制硬件的计算机运行 Windows,如果它以某种方式起作用的话。***

*** 确实在发挥作用。Macosx 似乎调用了退出函数,而 windows 则没有。

我决定写一个基本的测试:

from abc import *
import time

class Test(object):
    __metaclass__ = ABCMeta

    def __init__(self, *args, **kwargs):
        print("Init called.")

    def __enter__(self, *args, **kwargs):
        print("enter called")

    def __exit__(self, type, value, traceback):
        print("Exit called")

with Test() as t:
    time.sleep(100)
print("Should never get here.")
Run Code Online (Sandbox Code Playgroud)

我从 PyCharm 运行这段代码,当它在 sleep 语句中时,我按下了 pycharm 中的停止按钮。这是两者的输出:

麦考克斯:

Init called.
enter called
Exit called
Traceback (most recent call last):
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1591, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1018, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/.../Library/Preferences/PyCharmCE2017.1/scratches/scratch_25.py", line 22, in <module>
    time.sleep(100)
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)

视窗

Init called.
enter called

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

Bat*_*730 6

我在 PyCharm 错误跟踪器上找到了一个解决方法:

https://youtrack.jetbrains.com/issue/PY-17252

  • 在 PyCharm 中,转到帮助->编辑自定义属性
  • 同意创建 idea.properties 文件(如果有要求)
  • 添加以下行:kill.windows.processes.softly=true
  • 如果您使用 Numpy 或 Scipy,还需要添加以下环境变量:

    os.environ['FOR_DISABLE_CONSOLE_CTRL_HANDLER'] = "1"

  • 重启Pycharm

现在,当我应用此应用程序运行测试时(在 Windows 上!),我得到以下输出:

Init called.
enter called
Exit called
Traceback (most recent call last):
  File "C:/Users/.../.PyCharmCE2017.1/config/scratches/scratch_3.py", line 20, in <module>
    time.sleep(100)
KeyboardInterrupt

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)