首页 > 解决方案 > 线程在同一个循环中打印两次

问题描述

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

在烧瓶上:

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
...

标签: python-3.xmultithreadingflaskdebian

解决方案


我想通了为什么。

当在带有调试模式的 Flask 中运行诸如 threading 和 apscheduler 之类的模块时: on ... 它会发生。


推荐阅读