参考:http://www.voidcn.com/article/p-pmlncsni-bvo.html
按下Ctrl C时,我的while循环不会退出.它似乎忽略了我的KeyboardInterrupt异常.循环部分如下所示:
while True:try:if subprocess_cnt <= max_subprocess:try:notifier.process_events()if notifier.check_events():notifier.read_events()except KeyboardInterrupt:notifier.stop()breakelse:passexcept (KeyboardInterrupt, SystemExit):print \nkeyboardinterrupt ...
我正在使用py.test编写一些测试,在测试中我使用了funcargs.这些funcarg在conftest.py中定义了自己的设置和拆卸,如下所示:
conftest.py:def pytest_funcarg__resource_name(request):def setup():# do setupdef teardown():# do teardown我的问题是,当有人使用CTRL C停止测试执行时,所有内容都不为人所知.我知道有一个pytest_keyboard_interrupt钩子,但我不知道该怎么做.
很抱歉这个笨拙的问题.解决方法:您没有提供完整的示例,所以...
我写了很多小应用程序在哪里使用try:print "always does this until I Ctrl+C"Except KeyboardInterrupt:print "finish program"我刚开始不再使用IDLE,而是启动了PyScripter.但是,CTRL C不再起作用.使用内置解释器时是否仍然可以发送KeyboardInterrupt?解决方法:在PyScripter中,如果您只想终止正在运行的程序,则可以随时重新初始化远程引擎:
>应用程序运行菜单> Python引擎>重新初始化Python Engine或>解释器上下文菜单> Python引...
我在某处读到只在Python的主线程中引发KeyboardInterrupt异常.我还读到在子线程执行时主线程被阻塞.那么,这是否意味着CTRL C永远不会到达子线程.我尝试了以下代码:def main():try:thread = threading.Thread(target=f)thread.start() # thread is totally blocking (e.g., while True)thread.join()except KeyboardInterrupt:print "Ctrl+C pressed..."sys.exit(1)def f():while True:pass # do the actual work在这种情况下,CT...
有没有办法根据嵌入在Cython扩展中的循环中断(Ctrl C)Python脚本?
我有以下python脚本:def main():# Intantiate simulatorsim = 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'这将运行一个循环,它是C ...
我正在尝试两种方法来阻止无限循环运行:
> supervisor_1:以编程方式取消任务> supervisor_2:使用Ctrl C停止任务
虽然supervisor_2在中断时不会抛出任何错误,但我无法让supervisor_1获得任务被销毁但它正在等待!知道为什么吗?
这是代码:import asyncio
import aioredis
from functools import partialclass Listener:def __init__(self, redis_conn):self.redis_conn = redis_connasync def forever(self, loop_name):counter...
我正在运行这个简单的代码:import threading, timeclass reqthread(threading.Thread): def run(self):for i in range(0, 10):time.sleep(1)print('.')try:thread = reqthread()thread.start()
except (KeyboardInterrupt, SystemExit):print('\n! Received keyboard interrupt, quitting threads.\n')但是当我运行它时,它会打印出来$python prova.py
.
.
^C.
.
.
.
.
.
.
.
Exception KeyboardInterrupt in <module 'threadin...
我正在使用matplotlib实时监控实验,以在while循环中生成绘图.理想情况下,循环应该退出像KeyboardInterrupt这样的东西.这在Ubuntu测试中运行良好.在Windows 7中,使用ipython,它会以“终止批处理作业(是/否)?”退出.然后关闭翻译.我想避免这种行为,并在KeyboardInterrupt之后保持解释器打开.这是一个测试脚本.
[编辑2]:如果ipython作为ipython –pylab加载,此脚本在Windows中可以正常工作.import time
import numpy as np
import m...
我有一个python脚本,包含一个大循环读取文件和做一些东西(我使用几个包,如urllib2,httplib2或BeautifulSoup).
它看起来像这样:try:with open(fileName, 'r') as file :for i, line in enumerate(file):try:# a lot of code# ....# ....except urllib2.HTTPError:print "\n >>> HTTPError"# a lot of other exceptions# ....except (KeyboardInterrupt, SystemExit):print "Process manually stopped"raiseexcept Exception, e:pri...