调试模式下的键盘中断 PyCharm [英] Keyboard interrupt in debug mode PyCharm

查看:43
本文介绍了调试模式下的键盘中断 PyCharm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试模式下,有没有办法在 PyCharm IDE (3.1) 中发送键盘中断事件?

Is there any way to send a keyboard interrupt event in PyCharm IDE (3.1) while in the debugging mode?

推荐答案

不幸的是,没有简单的方法可以做到这一点.您将需要使用 psutilsignal 模块.为此,您需要安装 psutil,最好的方法是通过 pip:

Unfortunately, there is no simple way to do this. You will need to use psutil and the signal module. For this to work you need to install psutil and the best way to do that is through pip:

pip install psutil

所以,假设我们在这里展示了 A:

So, lets say we have here, exhibit A:

while True:
    try:
        time.sleep(3)
        print "Zzzz"
        time.sleep(3)
        print("gong!")
    except KeyboardInterrupt as e:
        print "Closed by an Interrupt"
        break

并且您正在 PyCharm 中运行它.确保您使用的解释器安装了 psutils.您可以检查:

And you're running this in PyCharm. Make sure that the interpreter you're using has psutils installed. You can check:

确保您已正确设置解释器:

Make sure you've set your interpreter correctly:

如果您还没有安装 psutil,您可以随时通过 安装 按钮安装.

If you haven't installed psutil, you can always do so though the Install button.

好的,现在我们已经设置好了一切,让我们调试程序:

Okay then, so now that we have everything set up, lets debug the program:

现在我们要做的就是获取进程 ID,我们可以在程序一开始就获取它:

Now all we have to do is get the process ID, and we can get that at the very start of the program:

所以,让我们启动我们的控制台,并发送一个信号:

So, lets fire up our console, and send a signal:

如果运行正常,您应该会看到 while 循环结束:

And if that worked properly, you should see the while loop ending:

您可以通过在控制台的启动脚本中添加一个发送中断的函数来进一步简化流程:

You can further streamline the process by adding a function to send an interrupt in the starting script for your console:

完成所有这些后,您需要做的就是调用 interrupt(<pid here>) 以在您的进程中调用键盘中断.

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.

希望能回答您的问题.

这篇关于调试模式下的键盘中断 PyCharm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆