Why is the code executed multiple times whenever Streamlit is started?
Asked Answered
K

2

10

When I launch my Streamlit application using the command "streamlit run streamlit_test.py", I noticed that the logs are printed multiple times. However, when I refresh the browser, the logs are only printed once. Here is my code:

import streamlit as st
from streamlit.logger import get_logger
import logging

LOGGER = get_logger(__name__)
LOGGER.setLevel(logging.DEBUG)
 
LOGGER.debug(f'start of streamlit_test')
st.write("hello")
LOGGER.debug(f'end of streamlit_test')

When I start Streamlit, the logs in my code are printed multiple times. Here are the logs:

2023-06-14 22:09:19.993 start of streamlit_test
2023-06-14 22:09:19.998 start of streamlit_test
2023-06-14 22:09:20.008 end of streamlit_test
2023-06-14 22:09:20.285 end of streamlit_test
2023-06-14 22:09:20.831 start of streamlit_test
2023-06-14 22:09:20.833 end of streamlit_test
2023-06-14 22:09:23.266 start of streamlit_test
2023-06-14 22:09:23.268 end of streamlit_test
2023-06-14 22:09:23.752 start of streamlit_test
2023-06-14 22:09:23.754 end of streamlit_test

When I refresh the browser, the logs are only printed once.Here are the logs:

2023-06-14 22:30:08.388 start of streamlit_test
2023-06-14 22:30:08.391 end of streamlit_test

I would like to know the reason behind this. Can someone help me? I would greatly appreciate it.

Kylakylah answered 14/6, 2023 at 14:38 Comment(3)
I couldn't reproduce this with streamlit==1.24.0.Lashelllasher
I've got the same behaviour when generating class objects that contain logging.Darwin
Having the same issue with class which generates logging. Also for some reason second session doesn't have state_session variables 🤔Pointless
D
2

It seems to be because streamlit is running your code on different threads, when available. My guess is that it has something to do with the way the underlying webserver works.

You can confirm it with the following:

import streamlit as st
from streamlit.logger import get_logger
import logging
import threading

LOGGER = get_logger(__file__)
LOGGER.setLevel(logging.DEBUG)

LOGGER.debug(f'start of streamlit_test, {threading.get_ident()}')
st.write("hellos")
LOGGER.debug(f'end of streamlit_test, {threading.get_ident()}')

And the output on my machine:

2024-01-05 14:51:30.542 start of streamlit_test, 140329869112896
2024-01-05 14:51:30.563 start of streamlit_test, 140329860720192
2024-01-05 14:51:30.564 end of streamlit_test, 140329860720192
2024-01-05 14:51:30.781 start of streamlit_test, 140329860720192
2024-01-05 14:51:30.783 end of streamlit_test, 140329860720192
2024-01-05 14:51:31.003 start of streamlit_test, 140329860720192
2024-01-05 14:51:31.004 end of streamlit_test, 140329860720192
2024-01-05 14:51:31.230 start of streamlit_test, 140329860720192
2024-01-05 14:51:31.233 end of streamlit_test, 140329860720192
2024-01-05 14:51:31.355 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.414 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.415 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.568 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.569 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.681 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.681 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.789 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.790 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.887 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.888 end of streamlit_test, 140329869112896
2024-01-05 14:51:31.978 start of streamlit_test, 140329869112896
2024-01-05 14:51:31.978 end of streamlit_test, 140329869112896
2024-01-05 14:51:32.079 start of streamlit_test, 140329869112896
2024-01-05 14:51:32.083 end of streamlit_test, 140329869112896
2024-01-05 14:51:32.180 start of streamlit_test, 140329869112896
2024-01-05 14:51:32.181 end of streamlit_test, 140329869112896
Disintegration answered 5/1, 2024 at 13:58 Comment(0)
N
2

Make sure you don't have multiple browser tabs with streamlit open

Northnortheast answered 28/6, 2024 at 13:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.