我想嘗試在 try 塊中捕獲 PyCharm 的停止信號(按下停止時),但我無法弄清楚這個信號是什么或如何在代碼中捕獲它。JetBrains 在他們的檔案中沒有提供對此的見解。
我試過捕捉它,
堆疊跟蹤將給出:
Traceback (most recent call last):
File "C:/path_to/your_module.py", line 2, in <module>
print("Press stop button here.")
File "C:/path_to/your_module.py", line 2, in <module>
print("Press stop button here.")
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 1589, in _pydevd_bundle.pydevd_cython_win32_39_64.ThreadTracer.__call__
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 929, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.trace_dispatch
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 920, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.trace_dispatch
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 317, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.do_wait_suspend
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1147, in do_wait_suspend
self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread)
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1162, in _do_wait_suspend
time.sleep(0.01)
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 2173, in <module>
main()
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 2164, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1476, in run
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1483, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents "\n", file, 'exec'), glob, loc)
File "C:/path_to/your_module.py", line 4, in <module>
raise Exception('Smelly socks').with_traceback(err.__traceback__)
File "C:/path_to/your_module.py ", line 2, in <module>
print("Press stop button here.")
File "C:/path_to/your_module.py ", line 2, in <module>
print("Press stop button here.")
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 1589, in _pydevd_bundle.pydevd_cython_win32_39_64.ThreadTracer.__call__
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 929, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.trace_dispatch
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 920, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.trace_dispatch
File "_pydevd_bundle\pydevd_cython_win32_39_64.pyx", line 317, in _pydevd_bundle.pydevd_cython_win32_39_64.PyDBFrame.do_wait_suspend
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1147, in do_wait_suspend
self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread)
File "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py", line 1162, in _do_wait_suspend
time.sleep(0.01)
Exception: Smelly socks
Process finished with exit code 1
堆疊跟蹤顯示的是當您按下停止按鈕時拋出的例外是 aKeyboardInterrupt但您也可以catch使用例外層次結構中更高的例外撰寫子句
(例如BaseException)結果將是相同的。
uj5u.com熱心網友回復:
我無法復制其他答案,因為停止按鈕作為鍵盤中斷發送。我確實相信停止按鈕可能在不同版本的 PyCharm 和作業系統上實作不同(我在 Linux 上,其中一個不同的答案似乎是 Windows,但我在這里的許多方面都不樂觀)
在我看來,正在發送一個終止信號,并且似乎并沒有像例外一樣捕獲它(對我來說)。但是,通過參考這篇討論在 Python 中捕獲終止信號并優雅地終止的文章,我能夠在一定程度上捕獲終止信號。下面是我使用的代碼。當我按下停止按鈕時,我看到了Hello world,但我沒有看到foobar。
此外,除錯器不能夠通過斷點抓到我在handler_stop_signals做這個,但我看到的文字。因此,根據您的需要,我不確定這是否真的能回答您的問題。另請注意,我實際上永遠不會撰寫這樣的代碼(使用全域變數),但這是我能想到的最簡單的答案。
import signal
import time
run = True
def handler_stop_signals(signum, frame):
global run
print("Hello world")
run = False
signal.signal(signal.SIGINT, handler_stop_signals)
signal.signal(signal.SIGTERM, handler_stop_signals)
while run:
try:
time.sleep(20) # do stuff including other IO stuff
except: BaseException as e:
print('foobar')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322001.html
