gpt4 book ai didi

python - 在 Python 中捕获 PyCharm 停止信号

转载 作者:行者123 更新时间:2023-12-04 16:36:14 33 4
gpt4 key购买 nike

我想尝试在 try 块中捕获 PyCharm 的停止信号(按下停止时),但我无法弄清楚这个信号是什么或如何在代码中捕获它。 JetBrains 在他们的文档中没有提供对此的见解。
我试过把它当作 BaseException 但它似乎根本不是一个异常(exception)。
这是否可以通过编程方式捕获?

最佳答案

当您按下停止按钮并记录或检查回溯时,您可以捕获异常。但是,当您运行调试器时 pydev.py 发生的情况是您正在启动一个客户端/服务器进程,该进程将作为客户端执行您的程序。请注意启动调试器时的控制台:

C:\path_to_your_python_binary\python.exe "C:\Program Files\JetBrains\PyCharm 2019.3.2\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 51438 --file C:/path_to/your_module.py C:\path_to_working_directory
Connected to pydev debugger (build 212.5080.64)
通过按停止,您实际上是在终止 pydev.py服务器进程,因此调试器不会在您的模块上保持卡住 except像往常一样的条款。您可以通过按下以下代码片段的打印语句中的停止按钮并打印或记录回溯来验证它(示例代码改编为 from this answer)。
try:
print("Press stop button here.")
except BaseException as err:
raise Exception('Smelly socks').with_traceback(err.__traceback__)
看截图:
enter image description here
堆栈跟踪将给出:
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
堆栈跟踪显示的是按下停止按钮时抛出的异常是 KeyboardInterrupt但你也可以写 catch使用在 Exception Hierarchy 中更高的异常的子句
(例如 BaseException )结果是一样的。

关于python - 在 Python 中捕获 PyCharm 停止信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69526398/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com