线程在同一循环中打印两次

Thread is printing two times at same loop

在每个循环中线程打印两次。在这种情况下,它每 3 秒打印两次。我希望它只打印一次。

在 Flask 上:

from flask import Flask
import threading
from time import sleep


app = Flask(__name__)

@app.route('/')
def index():
  return 'ok'

def func():
  count = 1
  while True:
    print(count)
    count += 1
    sleep(3)

t = threading.Thread(target=func)
t.setDaemon(True)
t.start()

if __name__ == '__main__':
  app.run(debug=True)

输出:

1
1
2
2
3
3
...

预期输出:

1
2
3
...

我明白了原因。

当 运行 Flask 中的线程和 apscheduler 等模块具有调试模式时:打开 ... 它发生了。