KeyboardInterrupts in Idle在90%的时间里对我来说是有效的,但我想知道为什么它们并不总是有效。在Idle中,如果我这样做
import time
time.sleep(10)
然后尝试使用Ctrl+C执行KeyboardInterrupt,它直到休眠10秒后才会中断进程。
同样的代码和通过Ctrl+C生成的KeyboardInterrupt可以立即在shell中运行。
如果我按CTRL+C,它会出错TypeError: end must be None or a string, not KeyboardInterrupt
为什么?
代码:
import sys
def erroring(a,b,c):
print(end=b)
sys.excepthook = erroring
while 1:pass
假设我有一堆进程,我想运行这些进程并检查它们是否正常:
procs = [fetcher_process, checker_process, publisher_process, manager_process]
try:
for p in procs:
p.run()
except Exception as e:
print("Oopsie")
exit()
for p in procs:
p.join()
这样的代码运行起来有缺陷,除非你点击了Ctrl+C,然后你在p.run()中得到了未处理的异常,尽管它肯定是在Keybo
这是一个测试用例。
try:
targ = raw_input("Please enter target: ")
except KeyboardInterrupt:
print "Cancelled"
print targ
当我按ctrl+c时,输出如下-
NameError: name 'targ' is not defined
我的意图是“取消”产出。当我试图在KeyboardInterrupt期间捕获一个raw_input时,有没有想过为什么会发生这种情况呢?
谢谢!
当我运行这段代码时: while 1:
try:
pass
except KeyboardInterrupt:
pass 然后我按ctrl+c来尝试通过KeyboardInterrupt来终止程序,我通常是成功的,但并不总是成功的。大多数情况下,我第一次尝试ctrl+c会终止程序,但有时,我需要按两次ctrl+c键。 将其与下面的代码进行比较: from time import sleep
while 1:
try:
sleep(0.0000000000000000000000000000000000000000000
我正在使用python线程检查本地SQL数据库中的值更改。我在处理Ctrl+C键盘中断的主程序上有错误。这将停止主代码,但线程仍在继续。当ctlr+c被按下时,我应该如何干净地停止线程。谢谢。线程是这样:
def getVars():
global setTemp
global pidControl
while True:
dB = MySQLdb.connect(host="localhost",
user="pythonUser",
我写了很多我用到的小应用
try:
print "always does this until I Ctrl+C"
Except KeyboardInterrupt:
print "finish program"
我刚刚开始不再使用IDLE并启动了PyScripter。然而,CTRL+C不再有效。在使用内置解释器的同时,是否仍然可以发送KeyboardInterrupt?
我试图在循环中执行一些操作(在本例中,打印为100),并通过按'Ctrl+C‘在任何时候取消计数。
我使用的测试代码如下所示,但是这不起作用(编辑:它在从终端启动的脚本中工作,但在我的IDE -机翼101的python外壳中运行时不工作)。KeyboardInterrupt没有捕获'Ctrl+C‘命令或任何其他键。
import time
i = 1
while (i < 101):
try:
print '%d percent complete' % i
time.sleep(0.5)
我有一个小游戏的代码,我想做,但是keyboardinterrupt的输出不起作用。我是python的新手,所以我愿意接受任何建议来修复我的代码。 代码: """
GAME
number 1-20 rapidly listing
if press Ctrl+C on 10, then win
if not, then lose and restart
"""
import time
print("Type Ctrl+C to stop")
print("If you land on 10, you win!"
我使用VS Code开发python程序。我发现它在使用外部终端时无法捕获ctrl+C密钥,但它与集成终端一起工作正常。 下面是我的python代码: import time
print('press enter to start,and press ctrl+C to stop:')
while True:
input("")
starttime = time.time()
print('Started')
try:
while True:
print('Co
由于一些奇怪的情况,我在一个服务器上工作,它在导入tensorflow时遇到了问题。
更具体地说,如果我尝试导入tensorflow,服务器就会卡住,我必须中断执行(通过按键盘上的Ctrl+C键)。在我中断它之后,一切都运行得很好,我的程序继续运行得很好。但是,我希望这个过程是自动化的,也就是说,在导入两秒钟之后,python程序会自动抛出一个"Ctrl+C“错误,并继续执行流程。我尝试过使用python的信号库来实现这一点,如下所示:
import signal
import os
def raise_timeout(*a,**kw):
print("tensorf
我使用的是Python3,下面的代码在运行异步工作线程池时尝试捕获CTRL+C。每个工作进程都会在无限循环中运行,等待消息出现在队列中进行处理。我不明白的是,为什么当我按CTRL+C时,except块中的log.info和print消息没有打印出来,它只是让我回到了xterm。with代码块是否在做一些事情来防止这种情况? def worker(q):
"""Worker to retrieve item from queue and process it.
Args:
q -- Qu
我如何修改这段代码(使用扭曲的代码)以使CTRL+C使其退出?我预计问题是doWork不会将控制权交还给反应堆,因此反应堆无法终止其执行。
def loop_forever():
i = 0
while True:
yield i
i += 1
time.sleep(5)
def doWork():
for i in loop_forever():
print i
def main():
threads.deferToThread(doWork)
reactor.run()
请注意,此代