当你点击Ctrl+ c,即KeyboardInterrupt在Python脚本中加注时,有没有办法让追溯不会出现?
Python程序通过Selenium WebDriver驱动Firefox.代码嵌入在try/ exceptblock中,如下所示:
session = selenium.webdriver.Firefox(firefox_profile)
try:
# do stuff
except (Exception, KeyboardInterrupt) as exception:
logging.info("Caught exception.")
traceback.print_exc(file=sys.stdout)
Run Code Online (Sandbox Code Playgroud)
如果程序因错误而中止,则WebDriver会话不会关闭,因此Firefox窗口保持打开状态.但是如果程序以KeyboardInterrupt异常中止,则Firefox窗口会关闭(我想因为WebDriver会话也被释放了),我想避免这种情况.
我知道两个异常都通过相同的处理程序,因为我"Caught exception"在两种情况下都看到了消息.
我怎么能避免关闭Firefox窗口KeyboardInterrupt?
我正在运行一个脚本,但它花了太长时间,所以我想终止脚本.然而,它已经计算了很多我理想情况下不想丢弃的数据.是否有ctrl-C将内部函数变量保存到工作区的替代方法?
理想情况下,我正在寻找一个Matlab键盘快捷键ctrl-C,但如果真的无法完成,也许有一种方法可以在我的函数脚本中执行此操作.知道如何让我的脚本做出反应ctrl-C,或者我可以取消的GUI元素,然后通过我的脚本保存变量?
我发现了一些类似的问题,但这些问题没有回答我的问题:
不同的问题,类似的答案:
编辑:
这个问题是不同的,因为提问者的问题是不同的:他们想知道错误在哪里,在我的案例中,Matlab已经说过了.我只想在工作内存中保留所有数据.
有没有办法Ctrl+C根据嵌入在Cython扩展中的循环中断()Python脚本?
我有以下python脚本:
def main():
# Intantiate simulator
sim = PySimulator()
sim.Run()
if __name__ == "__main__":
# Try to deal with Ctrl+C to abort the running simulation in terminal
# (Doesn't work...)
try:
sys.exit(main())
except (KeyboardInterrupt, SystemExit):
print '\n! Received keyboard interrupt, quitting threads.\n'
Run Code Online (Sandbox Code Playgroud)
这运行一个循环,它是C++ Cython扩展的一部分.然后,在按下时Ctrl+C,KeyboardInterrupt抛出但忽略,程序继续运行直到模拟结束.
我找到的工作是通过捕获SIGINT信号来处理扩展内的异常:
#include <execinfo.h>
#include <signal.h>
static void handler(int sig)
{
// Catch exceptions
switch(sig)
{
case SIGABRT:
fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr); …Run Code Online (Sandbox Code Playgroud) 我正在制作一个简单的多线程端口扫描器.它扫描主机上的所有端口并返回打开的端口.麻烦在于中断扫描.扫描完成需要花费大量时间,有时我希望在扫描过程中用Cc杀死程序.麻烦的是扫描不会停止.主线程被锁定在queue.join()上,并且忘记了KeyboardInterrupt,直到处理了队列中的所有数据,因此解除了主线程并正常退出程序.我的所有线程都被守护进来,所以当主线程死掉时,他们应该和他一起死掉.
我尝试使用信号库,没有成功.覆盖threading.Thread类和正常终止的添加方法不起作用...主线程在执行queue.join()时不会收到KeyboardInterrupt
import threading, sys, Queue, socket
queue = Queue.Queue()
def scan(host):
while True:
port = queue.get()
if port > 999 and port % 1000 == 0:
print port
try:
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#sock.settimeout(2) #you need timeout or else it will try to connect forever!
#sock.connect((host, port))
#----OR----
sock = socket.create_connection((host, port), timeout = 2)
sock.send('aaa')
data = sock.recv(100)
print "Port {} open, message: {}".format(port, data)
sock.shutdown()
sock.close()
queue.task_done()
except:
queue.task_done()
def main(host):
#populate queue
for i in range(1, …Run Code Online (Sandbox Code Playgroud) 当进入一个inteerupt处理程序时,我们首先在该cpu上"禁用中断"(使用类似x86上的cli指令).在禁用中断期间,假设用户按下键盘上的字母'a',这通常会导致中断.但由于中断被禁用,这是否意味着:
我正在尝试创建一个循环python函数,它执行任务并提示用户进行响应,如果用户在给定时间内没有响应,则序列将重复.
这是基于这个问题:如何设置raw_input的时间限制
任务由...表示some_function().超时是一个变量,以秒为单位.我有以下代码的两个问题:
无论用户是否提示,raw_input提示在指定的4秒时间后都不会超时.
当输入'q'的raw_input时(没有'',因为我知道键入的任何内容会自动输入为字符串),该函数不会退出循环.
`
import thread
import threading
from time import sleep
def raw_input_with_timeout():
prompt = "Hello is it me you're looking for?"
timeout = 4
astring = None
some_function()
timer = threading.Timer(timeout, thread.interrupt_main)
try:
timer.start()
astring = raw_input(prompt)
except KeyboardInterrupt:
pass
timer.cancel()
if astring.lower() != 'q':
raw_input_with_timeout()
else:
print "goodbye"
Run Code Online (Sandbox Code Playgroud)
`
所有文档告诉我们的是,
当用户按下中断键(正常
Control-C或Delete)时触发.在执行期间,定期检查中断.
但是从代码的角度来看,我何时可以看到这个异常?它是否在语句执行期间发生?只有在陈述之间?它可以发生在表达的中间吗?
例如:
file_ = open('foo')
# <-- can a KeyboardInterrupt be raised here, after the successful
# completion of open but prior to the try? -->
try:
# try some things with file_
finally:
# cleanup
Run Code Online (Sandbox Code Playgroud)
这段代码会在合适的时间内泄漏KeyboardInterrupt吗?或者是在执行某些语句或表达式时引发的?
我正在尝试做与这个问题完全相同的事情:
如何防止代码块被 Python 中的 KeyboardInterrupt 中断? (抱歉,还没有足够的代表在那里发表评论)
但是,在那里发布的两个最重要的答案都不适用于我。当我使用这些解决方案中的任何一个点击 CTRL+C 时,脚本仍然会立即关闭:
forrtl: error (200): program aborting due to control-C event
Run Code Online (Sandbox Code Playgroud)
我正在处理的代码相当长,包括相当多的导入模块。我假设这些模块之一干扰了 的正常行为是否正确KeyboardInterrupt?如果是这样,我怎样才能弄清楚是哪一个?
(我在 Windows 上运行 python 2.7.6,32 位)
谢谢。
在 Python 2 中有一个函数thread.interrupt_main(),KeyboardInterrupt当从子线程调用时,它会在主线程中引发异常。
这也可以_thread.interrupt_main()在 Python 3 中使用,但它是一个低级的“支持模块”,主要用于其他标准模块。
在 Python 3 中这样做的现代方法是什么,大概是通过threading模块,如果有的话?
python multithreading keyboardinterrupt python-multithreading python-3.x
python ×8
cython ×1
exception ×1
matlab ×1
python-2.7 ×1
python-3.x ×1
raw-input ×1
save ×1
traceback ×1
workspace ×1