为 Streamlit 应用程序自定义睡眠计时器¶
This topic describes how to set a custom sleep timer for a Streamlit app in Streamlit in Snowflake on warehouses.
关于 Streamlit 应用程序的睡眠计时器¶
Sleep timers only apply to Streamlit apps that use warehouse runtimes. Container runtimes are intended for long-running services and don’t support sleep timers.
The sleep timer is configured using the Streamlit app’s config.toml configuration file.
If your app was created with the ROOT_LOCATION parameter, you must use SQL to PUT the configuration
file in the app’s stage location. Otherwise, you can use SQL or the Snowsight Streamlit in Snowflake editor.
WebSocket 超时¶
当查看者打开 Streamlit 应用时,查看者的浏览器与 Streamlit 服务器之间会建立一个 WebSocket 连接。如果未设置自定义休眠定时器,应用将在 WebSocket 连接因无活动超时后自动挂起。在账户级别,默认 WebSocket 超时时间约为 15 分钟。您可以通过联系 Snowflake 支持部门,更改账户的所有 Streamlit 应用程序的 WebSocket 超时。
当您设置自定义休眠定时器时,该定时器会尝试使应用保持活动状态直至达到指定时限,随后尝试优雅地关闭连接。然而,根据用户的浏览器设置,非活动状态的浏览器标签页可能会暂停或延迟此计时机制。在此类情况下,应用仍将受 WebSocket 超时设置约束。因此,如果您设置的自定义休眠定时器短于 WebSocket 超时时长,在某些场景下您的应用可能不会如预期般快速自动挂起。为获得最佳效果,请将 WebSocket 超时设置为与您应用所使用的最短自定义休眠定时器时长相等。
此外,应用界面上任何鼠标移动都将同时重置 WebSocket 超时和自定义休眠定时器。
使用 Snowsight 设置自定义睡眠计时器¶
如果您的 Streamlit 应用使用仓库运行时,为降低代码仓库成本,可以为 Streamlit 应用设置自定义休眠定时器以实现自动暂停。如果您的应用程序是使用 ROOT_LOCATION 参数,则必须使用 PUT 命令而不是 Snowsight。
-
Sign in to Snowsight.
-
In the navigation menu, select Projects » Streamlit, and then select your Streamlit app.
-
In the upper-right corner, select Edit.
-
If
.streamlit/config.tomldoesn’t exist, in the file explorer on the left, select
» Create new file. Enter .streamlit/config.toml, and select Create. -
In the file explorer on the left, navigate to
.streamlit/config.toml. -
In the file editor, set the value of
streamlitSleepTimeoutMinutesin the[snowflake.sleep]table.For example, if you want the Streamlit app to auto-suspend after 8 minutes, add the following text to the
config.tomlfile:
使用 PUT 命令设置自定义睡眠计时器¶
If your Streamlit app was created with the ROOT_LOCATION parameter, you must use the PUT command to modify your app’s configuration file. If your Streamlit app was created with the FROM parameter, you can use either the PUT command or Snowsight to modify your app’s configuration file.
-
Create or modify the
config.tomlfile on your local machine to setstreamlitSleepTimeoutMinutesin the[snowflake.sleep]table.For example, if you want the Streamlit app to auto-suspend after 8 minutes, include the following text in your
config.tomlfile: -
Upload the
config.tomlfile to your app’s stage location. 如果您的应用程序是使用 ROOT_LOCATION 参数创建的,执行以下命令:
如果您的应用程序是使用 FROM 参数创建的,执行以下命令:
For more information about working with Streamlit files, see Create your Streamlit app.
Note
You can set the streamlitSleepTimeoutMinutes to any value between 5 to 240 minutes.
如果未创建配置文件来指定计时器,默认自动暂停时间为 15 分钟。