Python 出现 time.sleep 键盘中断错误

问题描述 投票:0回答:1

我在 python 脚本中连续调用一个函数 4 次,但参数不同:

testName = 'felb_ncq'
print "************ Test 1 %s ******************\n" % testName
tc.run_drivemaster_regression(testName)
time.sleep(30)

testName = 'felb_nonncq'
print "************ Test 2 %s ******************\n" % testName
tc.run_drivemaster_regression(testName)
time.sleep(30)

testName = 'be_sink_non_ncq'
print "************ Test 3 %s ******************\n" % testName
tc.run_drivemaster_regression(testName)
time.sleep(30)

第一次传递或迭代顺利,但在第二次函数调用中我收到以下错误:

Traceback (most recent call last):
  File "C:\Neptune_Step_2015\Tests\SVTestcases\TC-Regression\drivemaster_regression.py", line 48, in <module>
time.sleep(30)
KeyboardInterrupt

为什么当我根本没有中断执行时却收到键盘中断错误?

一个观察结果是:如果我使用单独的参数运行四个不同的脚本,而不是调用该函数 4 次,那么我所有四遍都运行良好。因此,在同一个脚本中连续调用它们时一定存在一些问题(例如竞争条件)。

python python-2.7 python-3.x
1个回答
0
投票

当您使用 Ctrl+C 中断脚本的执行时,会出现您看到的错误 (KeyboardInterrupt)。这是预期的行为,它表明脚本已被手动终止。

如果您想优雅地处理 KeyboardInterrupt 异常并避免在脚本中断时显示回溯,您可以捕获 KeyboardInterrupt 异常并干净地退出脚本。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.