首页 > 解决方案 > Dash 应用程序在加载服务器后重新执行早期缓存步骤

问题描述

当我启动我的 Dash 应用程序时,我会执行一系列一次性任务 - 提取我需要的数据等。但是,Dash 似乎执行了两次:一次是我要求他们在脚本中发生,并且在加载服务器后第二次发生。

这是我正在谈论的一个简单示例:

module_link_test_source.py

print("Source module")

def source_module_function():
    print("Source function")

    return 1

module_link_test_destination.py

import module_link_test_source as source
import dash
import dash_html_components as html

print("Destination module")
app = dash.Dash(__name__)

function_run = source.source_module_function()

app.layout = html.Div()

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

当我运行时python module_link_test_destination.py,这是控制台吐出的内容:

Source module
Destination module
Source function
Dash is running on http://127.0.0.1:8050/

 * Serving Flask app "module_link_test_destination" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
Source module
Destination module
Source function

我只希望这些print()语句每个都执行一次。如何使这些步骤只发生一次?

标签: pythonplotly-dash

解决方案


我相信您会看到这种行为,因为您正在调试模式下运行。尝试debug=Falserun_server通话中设置。


推荐阅读