我需要使用ctrl c从time.sleep()中断.
While 1:
time.sleep(60)
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,当控件进入time.sleep函数时,需要经过整整60秒才能处理CTRL C
有没有优雅的方式来做到这一点.这样我即使在控制时间内也可以中断.睡眠功能
编辑
我在一个遗留实现上测试它,在Windows 2000上使用python 2.2导致了所有的麻烦.如果我使用了更高版本的python CTRL C会中断sleep().我通过在for循环中调用sleep(1)来快速破解.这暂时解决了我的问题
在pycharm中调试代码时,按Ctrl + C时,我的python try / except循环似乎不会触发键盘中断。我的代码如下所示:
numbers = []
loop = True
try:
# ===========SUBROUTINES==================
def help():
print("To view the list type 'view'"
"\n To add an item type 'add'"
"\n To remove an item type 'remove'"
"\n To exit type exit or Ctrl + c can be used at any time")
# =========SUBROUTENES END===============
while loop:
task = input("What do you want to do? Type \"help\" for help:- ")
if task == 'help':
help()
else:
print("Invalid return please try …
Run Code Online (Sandbox Code Playgroud)