标签: keyboardinterrupt

如何在Windows中使用子程序Popen.send_signal(CTRL_C_EVENT)时获得所需的结果?

在根据文档的Windows中的python 2.7中,您可以发送CTRL_C_EVENT (Python 2.7 Subprocess Popen.send_signal文档). 但是,当我尝试它时,我没有在子进程中收到预期的键盘中断.

这是父进程的示例代码:

# FILE : parentProcess.py
import subprocess
import time
import signal

CREATE_NEW_PROCESS_GROUP = 512
process = subprocess.Popen(['python', '-u', 'childProcess.py'],
                       stdin=subprocess.PIPE,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT,
                       universal_newlines=True,
                       creationflags=CREATE_NEW_PROCESS_GROUP)
print "pid = ", process.pid
index = 0
maxLoops = 15
while index < maxLoops:
    index += 1
    # Send one message every 0.5 seconds
    time.sleep(0.5)
    # Send data to the subprocess
    process.stdin.write('Bar\n')
    # Read data from the subprocess
    temp = process.stdout.readline()
    print temp,
    if (index == 10):
        # …
Run Code Online (Sandbox Code Playgroud)

python windows subprocess popen keyboardinterrupt

5
推荐指数
2
解决办法
1万
查看次数

在提升tkinter帧之前,不会处理tkinter键盘中断

我有一个用python + tkinter编写的GUI应用程序。在我的工作流程中,我通常从命令行启动gui,在gui中做一些事情,然后发现自己导航到其他终端窗口来做一些工作。不可避免地,我想在某个时候关闭GUI,出于习惯,我经常只是导航到启动GUI的终端并发送KeyboardInterrupt(Ctrl-c)。但是,直到我在窗口管理器中举起GUI窗口时,才收到此中断。有谁知道为什么会这样吗?如果gui是在单个函数中启动的,那么有没有简单的解决方法- multiprocessing也许吗?

python tkinter keyboardinterrupt

5
推荐指数
1
解决办法
1702
查看次数

捕获键盘中断以阻止 Python 多处理工作者处理队列

从在 stackoverflow 上找到的几篇文章中,我创建了这段代码。

设想

我想要一个 multiprocessing.queue 几个工人“听”

在键盘中断的情况下,主进程不应再将新项目放入队列中,并且在哨兵对象的帮助下,应该优雅地停止工作进程。

问题

我使用的当前版本的问题

signal.signal(signal.SIGINT, signal.SIG_IGN) 
Run Code Online (Sandbox Code Playgroud)

忽略 Ctrl + C 就是它也被主进程忽略了。

有任何想法吗 ?我需要使用多处理工作池吗?一些例子表明我可能不得不这样做。那我还可以使用队列吗?

from multiprocessing import Pool, Process,Queue
import time
import signal
# http://docs.python.org/3.1/library/multiprocessing.html#multiprocessing.Queue
# http://docs.python.org/3.1/library/multiprocessing.html#multiprocessing.Process


class Worker(Process):
    def __init__(self, queue,ident):
        super(Worker, self).__init__()
        # Ignore Signals
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        self.queue= queue
        self.idstr= str(ident)
        print "Ident" + self.idstr

    def run(self):
        print 'Worker started'
        # do some initialization here

        print 'Computing things!'
        for data in iter( self.queue.get, None ):
            print "#" + self.idstr + " : " …
Run Code Online (Sandbox Code Playgroud)

python multithreading exception keyboardinterrupt

5
推荐指数
1
解决办法
3469
查看次数

Python主线程中断

谁能解释一下interrupt_main()方法在Python中是如何工作的?

我有这段Python代码:

import time, thread

def f():
    time.sleep(5)
    thread.interrupt_main()

def g():
    thread.start_new_thread(f, ())
    time.sleep(10)
print time.time()
try:
    g()
except KeyboardInterrupt:
    print time.time()
Run Code Online (Sandbox Code Playgroud)

当我尝试运行它时,它给我以下输出:

1380542215.5
# ... 10 seconds break...
1380542225.51
Run Code Online (Sandbox Code Playgroud)

但是,如果我手动中断程序(CTRL-C),线程会被正确中断:

1380542357.58
^C1380542361.49
Run Code Online (Sandbox Code Playgroud)

为什么线程中断仅在第一个示例中的10秒(而不是5)之后发生?

我在Python邮件列表中找到了一个古老的线程,但它几乎没有解释.

python multithreading keyboardinterrupt

5
推荐指数
1
解决办法
3053
查看次数

Selenium:在不关闭浏览器的情况下退出Python脚本

我使用以下代码处理使用Ctrl+ C终止正在运行的Python脚本的情况。

except KeyboardInterrupt:
    print "ABORTED"
Run Code Online (Sandbox Code Playgroud)

但是,这也会终止我的Selenium WebDriver浏览器。

有没有办法终止脚本并使浏览器保持活动状态,以便我可以继续使用它?

我通常要做的是通过Ctrl+ 暂停脚本Z。不幸的是,这经常导致浏览器死机并且不响应。

python selenium sys keyboardinterrupt

5
推荐指数
1
解决办法
3672
查看次数

SIGINT 在 python 中本质上是不可靠的吗?

我有一个依赖于SIGINT正常关闭的应用程序。我注意到它每隔一段时间就会继续运行。原因原来是xml/etree/ElementTree.py.

如果SIGINT在清理生成器时到达,则忽略所有异常(回想一下 的默认操作SIGINT是引发 KeyboardInterrupt)。这不是这个特定生成器或一般生成器所独有的。

从 python 文档:

“由于__del__()调用方法的不稳定环境,在执行过程中发生的异常将被忽略,而是向 sys.stderr 打印警告”

在超过五年的 Python 编程中,这是我第一次遇到这个问题。

如果垃圾收集可以在任何时候发生,那么SIGINT理论上也可以在任何时候被忽略,我永远不能依赖它。那是对的吗?我一直很幸运吗?

或者是关于这个特定的包和这个特定的生成器?

python garbage-collection generator sigint keyboardinterrupt

5
推荐指数
0
解决办法
53
查看次数

twisted - 通过KeyboardInterrupt中断回调

我正在使用Twisted在回调中的for循环中重复一个任务,但是如果用户通过Ctrl-C发出KeyboardInterrupt,则希望反应器在回调中打破循环(一).根据我测试的结果,反应器仅在回调结束时停止或处理中断.

有没有办法在回调运行过程中向回调或错误处理程序发送KeyboardInterrupt?

干杯,

克里斯

#!/usr/bin/env python

from twisted.internet import reactor, defer


def one(result):
    print "Start one()"
    for i in xrange(10000):
        print i
    print "End one()"
    reactor.stop()


def oneErrorHandler(failure):
    print failure
    print "INTERRUPTING one()"
    reactor.stop()    


if __name__ == '__main__':

    d = defer.Deferred()
    d.addCallback(one)
    d.addErrback(oneErrorHandler)
    reactor.callLater(1, d.callback, 'result')

    print "STARTING REACTOR..."
    try:
        reactor.run()
    except KeyboardInterrupt:
        print "Interrupted by keyboard. Exiting."
        reactor.stop()
Run Code Online (Sandbox Code Playgroud)

twisted reactor keyboardinterrupt

4
推荐指数
2
解决办法
5011
查看次数

在KDE Python应用程序中处理KeyboardInterrupt?

我正在研究PyKDE4/PyQt4应用程序,Autokey,我注意到当我发送程序一个CTRL + C时,键盘中断在我与应用程序交互之前不会被处理.单击菜单项或更改复选框.

lfaraone@stone:~$ /usr/bin/autokey
^C^C^C
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/autokey/ui/popupmenu.py", line 113, in on_triggered
    def on_triggered(self):
KeyboardInterrupt
^C^C^C
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/autokey/ui/configwindow.py", line 423, in mousePressEvent
    def mousePressEvent(self, event):
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)

尽管在/ usr/bin/autokey中有以下内容:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from autokey.autokey import Application

a = Application()
try:
    a.main()
except KeyboardInterrupt:
    a.shutdown()
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

为什么没有捕获KeyboardInterrupt:

  • 当我发出它时,而不是当我接下来在GUI中执行操作时
  • 通过最初的try/except子句?

使用Python 2.6运行Ubuntu 9.04.

python pyqt pykde keyboardinterrupt autokey

3
推荐指数
1
解决办法
1749
查看次数

PyScripter - 无法使用KeyboardInterrupt终止运行

我在我使用的地方写了很多小应用程序

try:
   print "always does this until I Ctrl+C"


Except KeyboardInterrupt:
   print "finish program"
Run Code Online (Sandbox Code Playgroud)

我刚刚开始使用IDLE并启动了PyScripter.但是CTRL + C不再有效.是否仍然可以KeyboardInterrupt使用内置解释器发送一段时间?

python pyscripter keyboardinterrupt

3
推荐指数
1
解决办法
3284
查看次数

Python中延迟键盘中断是程序的重要部分

对于程序的重要部分(在我的示例中是一个循环)延迟键盘中断的方法是什么?

我想下载(或保存)许多文件,如果需要很长时间,我想在下载最近的文件后完成程序。

我需要像在 Python 中捕获键盘中断的答案中一样使用信号模块而不需要 try- except 吗?我可以使用信号处理程序将全局变量设置为 True 并在为 True 时中断循环吗?

原周期为:

for file_ in files_to_download:
    urllib.urlretrieve("".join(baseurl, file_), os.path.join(".", file_)) 
Run Code Online (Sandbox Code Playgroud)

python keyboardinterrupt

3
推荐指数
1
解决办法
1468
查看次数