Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the code executed multiple times whenever Streamlit is started?

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.

like image 919
Juan Gong Avatar asked May 01 '26 17:05

Juan Gong


2 Answers

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
like image 142
artygo Avatar answered May 05 '26 22:05

artygo


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

like image 33
Lyubomyr Ivanitskiy Avatar answered May 05 '26 23:05

Lyubomyr Ivanitskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!