python中断言_如何在python中中断阻塞方法?(How can I interrupt a blocking method in python?)...

如何在python中中断阻塞方法?(How can I interrupt a blocking method in 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 "", line 1, in

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 "", line 1, in

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 ..

原文:https://stackoverflow.com/questions/7315981

更新时间:2020-02-28 12:31

最满意答案

您可以使用Ctrl + \终止Python解释器。

这将发送SIGQUIT而不是SIGINT 。

You can kill the Python interpreter with Ctrl+\.

This will send a SIGQUIT instead of SIGINT.

2011-09-06

相关问答

如果中断线程不是一个选项,另一个是在队列中放置一个“标记”或“命令”对象,MyObjHandler将被识别,并且突破循环。 If interrupting the thread is not an option, another is to place a "marker" or "command" object on the queue that would be recognized as such by MyObjHandler and break out of the loop.

您可以从另一个线程调用close() ,并且accept()调用将抛出SocketException异常 。 You can call close() from another thread, and the accept() call will throw a SocketException.

您应该传递对您希望在线程中启动的方法的引用。 相反,您正在调用线程,并将从该方法返回的数据传递给threading.Thread()调用。 简而言之,您的代码应该变为: if (A==0):

ConnectionProcessorA = threading.Thread(target=self.ConnectionProcessorA)

ConnectionProcessorA.start()

A=1

elif (A==1)

...

只要您在主线程中运行doA ,就可以使用threading.Timer调用thread.interrupt_main 。 请注意,你所期望的语法是不可能的,因为Python(像大多数语言,除了例如Haskell)是“渴望的” - 参数在执行调用之前完全计算,所以self.doA()会在call方法之前运行完成有任何机会去做任何事情! 你将不得不使用一些语法如 CoolIt.call(self.doA, args=(), timeout=100)

以便在使用给定参数执行对doA的调用之前 , ca

...

您可以使用Ctrl + \终止Python解释器。 这将发送SIGQUIT而不是SIGINT 。 You can kill the Python interpreter with Ctrl+\. This will send a SIGQUIT instead of SIGINT.

Peter Lawrey描述的一种解决方案也是关闭插座。 使用nio,您也可以使用可中断的SocketChannel ,并允许应用Java的标准中断模型 。 调用Thread对象的interrupt方法会抛出InterruptedException,甚至会阻止阻塞IO操作。 A solution, described by Peter Lawrey and also seen here is to close the socket. With nio, You could also use a S

...

我只想定义一个函数来向进程发送一个中断。 我使用anaconda-mode不是elpy ,所以我不确定elpy使用什么来获得关联的process-buffer ,但python-mode有python-shell-get-process-or-error 。 然后,您可以将密钥Cc Ca绑定到以下函数中,例如,在python-mode-map中。 (defun my-interrupt ()

"Send an interrupt signal to python process"

(int

...

阻塞同步方法是否可以中断? 不 ,但下面是实现你想做的最好方法! public class Foo {

private final Lock lock = new ReentrantLock();

public void bar() throws InterruptedException {

lock.lockInterruptibly();

try {

// Do stuff

}finally {

...

以下是您的程序如何运行,带有时间戳: 0000 StartMain

0000 Startl

3000 EndMain

6000 End1

6000 Start2

6000 End2

为什么(括号中的时间戳)? [0000]主要启动Thread1,其获取锁并睡眠6秒 [1000] main启动Thread2,它无法获取Thread1持有的锁定6秒 [2000]主要中断Thread2将其中断标志设置为true,但是Thread2正在等待锁定而对它没有任何作用 [3000]主要目的 [60

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值