MOngo connection is creating multiple time while running streamlit application?

I all I am working on creating new application, where the application should create a database connection based on the input submitted from the streamlit app.

I have created code like below, but while debugging the log I observed database connection is created multiple times. Wondering why the database connection is created for multiple times.

st.title("Sample Application")

    # ---- Streamlit Side Bar Configurations
    with st.sidebar:
        st.header("Select Options")

        # -- Dropdown to select the environment
        s_environment = st.selectbox(
            label="Choose Environment",
            options=["Development", "Stage", "Production"]
        )
        # -- Dropdown to select the environment
        b_filter_result = st.button("Get Filters", type="primary")

        if b_filter_result:
            o_mongo_con = connect_to_mongo(s_environment, o_logger)
            l_user_name = get_all_bus_users(o_mongo_con, o_logger)
            l_users_name = set(l_user_name)

Log details:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,416, main, INFO, Connecting to Mongo:
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883
2023-12-09 23:47:12,445, main, INFO, Total time taken to create Mongo connection: 0.028867006301879883

type or paste code here

Could some one please assist why is wrong in my code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.