如何中断Python中的阻塞方法?
通常我可以使用 Ctrl+C 中断操作,但有时当我使用线程时它不起作用 - 下面的示例。
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.sleep(100)
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
K eyboardInterrupt
>>> import Queue
>>> q = Queue.Queue(maxsize=3)
>>> q.put(0)
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
^C^C^C^C^C^C^C^C
^C^C^C
^C^C
^C
@*#()#@#@$!!!!!
编辑:有办法回到口译员那里吗?到目前为止的解决方案完全杀死了 python 和你现有的命名空间..
Usually I can interrupt stuff with Ctrl+C, but sometimes when I'm using threads it doesn't work - example below.
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.sleep(100)
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
K eyboardInterrupt
>>> import Queue
>>> q = Queue.Queue(maxsize=3)
>>> q.put(0)
>>> q.put(1)
>>> q.put(2)
>>> q.put(3)
^C^C^C^C^C^C^C^C
^C^C^C
^C^C
^C
@*#()#@#@$!!!!!
edit: Is there a way to get back to the interpreter? Solutions so far kill python completely and your existing namespace ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Ctrl+\ 终止 Python 解释器。
这将发送
SIGQUIT
而不是SIGINT
。You can kill the Python interpreter with Ctrl+\.
This will send a
SIGQUIT
instead ofSIGINT
.^C 失败的快速解决方法是首先使用 ^Z 暂停所有线程的进程,然后终止它。
当 ^C 失败时,这在 Linux 中的许多情况下都有效,并且正如我刚刚测试的那样,它在这里也有效(在 Python v.2.6.5 上测试):
A quick workaround for ^C failing is suspending the process with all threads first with ^Z and then killing it.
This works in Linux for many cases when ^C fails, and as I've just tested it works here too (tested on Python v.2.6.5):
一个懒惰的方法是打开另一个窗口。
执行
ps
来获取 PID。执行
kill
来终止有问题的进程。A lazy way to do this is to open another window.
Do
ps
to get the PID.Do
kill
to kill the offending process.