Dash 应用程序在加载服务器后重新执行早期缓存步骤

Dash app re-performs early caching steps after loading server

当我启动我的 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() 语句每个都执行一次。如何让这些步骤只发生一次?

我相信您看到此行为是因为您 运行 处于调试模式。尝试在 run_server 调用中设置 debug=False