python-3.x - 线程在同一个循环中打印两次
问题描述
在每个循环内部,线程打印两次。在这种情况下,它每 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
...
解决方案
我想通了为什么。
当在带有调试模式的 Flask 中运行诸如 threading 和 apscheduler 之类的模块时: on ... 它会发生。
推荐阅读
- javascript - 如何在 Selenium Python 中使用 Javascript 单击元素
- python - Django rest 框架发布请求被禁止(未设置 CSRF cookie。)
- c - 如何避免 TSP 问题中的运行时错误?
- django - Django 测试 - 使用经过身份验证的用户访问站点
- graphics - 着色器中的 3 字节顶点属性是否存在性能损失?
- c++ - Foreach 循环比传统的 for 循环使用更多的堆栈内存?
- python - 绘制数据框时 X 轴标签重叠
- python - 使用 Python 在 HTML 脚本的标签中获取文本
- kubernetes - 自动将新创建的虚拟机/服务器作为 kubernetes 节点加入到主节点
- reactjs - 创建反应应用程序而不删除现有文件