Streamlit and Multithreading Shutdown Events and Multiinitialisation
See original GitHub issueSummary
1.While working with Streamlit I needed a thread to run in the Background, however this thread does not shut down when pressing CTRL + C in the terminal. Thus i tried setting a shutdown event with Signal.SIGTERM, this is not possible either, I get the “ValueError: signal only works in main thread”. Is there a function which i can implement, which will be called upon Pressing CTRL+C in the streamlit console?
2.Another Problem which occures , is that the Manager and thus the thread is sometimes initialized twice. It doesnt happen that often with this small snippet, but in a bigger project it happened much more frequently. At some point it happened upon every start of the streamlit Server, a “fix” was to put a time.sleep(2) in the main function, rendering the interface rather useless though. After a restart of the Workstation, the Problem seems to happen less frequently.
EDIT: The second Problem seems to correlate with the runtime of the Computer. When I start off in the morning I only get one Initialisation, 8hours later however it initializes with up to 4 Instances!
EDIT2: In the second Problem, the number of initialized Instances correlates to the number of Clients/Webpages which try to connect to the Streamlit Server!
Steps to reproduce
The Code below is a Minimum working example uncomment the signal.signal(signal.SIGTERM, manager.terminate) line to see the secondary Problem.
import streamlit as st
import threading
import time
import signal
class SomeThread(threading.Thread):
def __init__(self, dummy_list):
super(SomeThread, self).__init__()
self.dummy_list = dummy_list
self.sleep_time = 1
self.shutdown=False
def run(self):
while not self.shutdown:
print(self.dummy_list)
time.sleep(2)
def terminate(self):
self.shutdown = True
@st.cache(allow_output_mutation=True)
class Manager:
def __init__(self):
print("Manager is beeing Initialized")
self.dummy_list = []
self.thread = SomeThread(self.dummy_list)
self.thread.start()
def do_smth(self, item):
self.dummy_list.append(item)
print("blub")
def terminate(self):
self.thread.terminate()
def main():
manager = Manager()
signal.signal(signal.SIGTERM, manager.terminate)
selected = st.selectbox("Dummy", [1, 23, 4])
if st.button("add"):
manager.do_smth(selected)
if __name__=="__main__":
main()
Expected behavior:
- Upon pressing CTRL + C I expect the thread to stop and streamlit to shutdown.
- I expect the Manager object to be initialized only once
Actual behavior:
- Both the Thread and streamlit continue to run.
- sometimes the Manager object and thread is initialized twice
Is this a regression?
no
Debug info
- Streamlit version: 0.63.0
- Python version: 3.6.9
- pipEnv
- OS version: Ubuntu
- Browser version: Edge, firefox
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:15 (3 by maintainers)
Top GitHub Comments
Thanks for the code example. What is your overall use case for using multiple threads in this manner?
Hey @kmcgrady thank you for taking the time and looking into it and finding the bug. It is good to know that you are working on it, let me know once it is ready. As of right now I am aware of the bug and can avoid it the majority of the time. Therefore I dont neccesarily need a workaround for the time beeing. Thanks!