简体   繁体   English

仅在Keyboardinterrutps之后累积回溯

[英]Traceback accumulation only after Keyboardinterrutps

This is my code: 这是我的代码:

import autopy
import time
import math
width, height = 400, 400
a, b = 200, 200
r = 150
def drawCicle():
    for x in range(0, 3):
        for angle in range(0, 360, 1):
            x = r * math.sin(math.radians(angle)) + a
            y = r * math.cos(math.radians(angle)) + b
            autopy.mouse.move(int(x),int(y))
            time.sleep(0.002)
def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles')
        print('moving once more in 10 seconds...')
        counter = 0
        time.sleep(10)
        mouseMove()

if __name__ == "__main__":
    mouseMove()

The strange thing is that the code runs just fine. 奇怪的是,代码运行得很好。 I only get tracebacks after breaking my loop with a KeyboardInterrupt , spilling out accumulated tracebacks from each loop that ran like this: 只有在使用KeyboardInterrupt中断循环后,我才会获得回溯,这样从每个循环中溢出累积的回溯,如下所示:

Traceback (most recent call last):
  File "test.py", line 27, in <module>
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 24, in mouseMove
    mouseMove()
  File "test.py", line 23, in mouseMove
    time.sleep(10)
KeyboardInterrupt

This specifically happens only if I break the code manually, can anyone shed some light as to what best practice I am ignoring? 仅当我手动破坏代码时,这种情况才会发生,任何人都可以阐明我所忽略的最佳实践吗?

If you want to see the output from your print statements immediately, just use flush=True . 如果要立即查看打印语句的输出,请使用flush=True

Whether output is buffered is usually determined by file , but if the flush keyword argument is true, the stream is forcibly flushed. 输出是否被缓冲通常由file决定,但是如果flush关键字参数为true,则将强制刷新流。


def mouseMove():
    counter = 0
    while counter < 4:
        drawCicle()
        counter += 1
    else:
        print('Drawing ' + str(counter) + ' circles', flush=True)
        print('moving once more in 10 seconds...', flush=True)
        counter = 0
        time.sleep(10)
        mouseMove()

Exception tracebacks are only printed when you exit your program with an exception. 仅当您退出程序并出现异常时,才打印异常回溯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM