Stopping a python process so that context managers still call __exit__ (in windows)

后端 未结 1 911
日久生厌
日久生厌 2021-02-06 16:24

How can I stop a python process in such a way that any active context managers will gracefully call their __exit__ function before closing?

I use context managers (__ent

相关标签:
1条回答
  • 2021-02-06 17:07

    I found a workaround on the PyCharm bug tracker:

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

    • In PyCharm go to help->Edit Custom Properties
    • Agree to create the idea.properties file (if it asks)
    • Add the following line: kill.windows.processes.softly=true
    • If you use Numpy or Scipy, you will also need to add the following environment variable:

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

    • Restart Pycharm

    Now when I run my test with this applied (on Windows!) I get the following output:

    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
    
    0 讨论(0)
提交回复
热议问题