[英]Keyboard interrupt in debug mode PyCharm
Is there any way to send a keyboard interrupt event in PyCharm IDE (3.1) while in the debugging mode? PyCharm IDE (3.1) 在调试模式下有没有办法发送键盘中断事件?
Unfortunately, there is no simple way to do this. 不幸的是,没有简单的方法可以做到这一点。 You will need to use
psutil
and the signal
module. 您将需要使用
psutil
和signal
模块。 For this to work you need to install psutil
and the best way to do that is through pip
: 为此,您需要安装
psutil
,最好的方法是通过pip
:
pip install psutil
So, lets say we have here, exhibit A: 所以,让我们说在这里,展示A:
while True:
try:
time.sleep(3)
print "Zzzz"
time.sleep(3)
print("gong!")
except KeyboardInterrupt as e:
print "Closed by an Interrupt"
break
And you're running this in PyCharm. 你在PyCharm中运行它。 Make sure that the interpreter you're using has
psutils
installed. 确保您使用的解释器已安装
psutils
。 You can check: 你可以查看:
Make sure you've set your interpreter correctly: 确保您正确设置了解释器:
If you haven't installed psutil
, you can always do so though the Install button. 如果尚未安装
psutil
,则可以通过“ 安装”按钮始终执行此操作。
Okay then, so now that we have everything set up, lets debug the program: 好吧那么,现在我们已经设置了所有内容,让我们调试程序:
Now all we have to do is get the process ID, and we can get that at the very start of the program: 现在我们所要做的就是获取进程ID,我们可以在程序的最开始处得到它:
So, lets fire up our console, and send a signal: 所以,让我们启动我们的控制台,并发送一个信号:
And if that worked properly, you should see the while loop ending: 如果这个工作正常,你应该看到while循环结束:
You can further streamline the process by adding a function to send an interrupt in the starting script for your console: 您可以通过在控制台的启动脚本中添加一个发送中断的函数来进一步简化该过程:
Once you're done with all of that, all you need to do is call interrupt(<pid here>)
to call a keyboard interrupt on your process. 完成所有这些后,您需要做的就是调用
interrupt(<pid here>)
来调用进程上的键盘中断。
I hope that answers your question. 我希望能回答你的问题。
This is a bug in PyCharm. 这是PyCharm中的一个错误。 See: http://youtrack.jetbrains.com/issue/PY-4840
请参阅:http: //youtrack.jetbrains.com/issue/PY-4840
Keyboard interrupt is a SIGINT. 键盘中断是SIGINT。 On unix systems you can either go to the command line and do:
在unix系统上,您可以转到命令行并执行以下操作:
$ kill -INT <pid>
or in python: 或者在python中:
import os, signal
os.kill(<pid>,signal.SIGINT)
As mentioned in this comment - Why doesn't this python keyboard interrupt work? 正如本评论中所提到的 - 为什么这个python键盘中断不起作用? (in pycharm) :
(在pycharm中) :
In recent versions of PyCharm, you can enable
Emulate terminal in output console
in your Run Configuration - this allows Ctrl + C in the Run console to send a keyboard interrupt.在最新版本的PyCharm中,您可以在运行配置
Emulate terminal in output console
中启用Emulate terminal in output console
- 这允许在运行控制台中按Ctrl + C发送键盘中断。
Tested with PyCharm 2018.3 (Community Edition): 使用PyCharm 2018.3(社区版)测试:
I came through this message while searching Pycharm's bug tracking for this issue: https://youtrack.jetbrains.com/issue/PY-4840 我在搜索Pycharm的错误跟踪时遇到了这个问题: https ://youtrack.jetbrains.com/issue/PY-4840
If you are using version Pycharm 3, this might help, it worked for me. 如果您使用的是Pycharm 3版本,这可能有所帮助,它对我有用。
One of the comments in the tracker: 'I have actually found out that Ctrl+C does eventually stop the running script, but you have to first write a letter into the console while it's running. 跟踪器中的一条评论:'我实际上发现Ctrl + C最终会停止正在运行的脚本,但是你必须先在控制台运行时写一封信。 So click into the console window, hit any key and then press Ctrl-C.
因此,单击控制台窗口,按任意键,然后按Ctrl-C。 In other words, it looks like a problem of GUI frame getting focus.'
换句话说,它似乎是一个GUI框架得到关注的问题。
Sounds silly but this worked for me.听起来很傻,但这对我有用。
Exit PyCharm and delete.idea folder from finder and startup PyCharm again.退出 PyCharm 并从 finder 中删除 .idea 文件夹并再次启动 PyCharm。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.