question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Streamlit and Multithreading Shutdown Events and Multiinitialisation

See original GitHub issue

Summary

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:

  1. Upon pressing CTRL + C I expect the thread to stop and streamlit to shutdown.
  2. I expect the Manager object to be initialized only once

Actual behavior:

  1. Both the Thread and streamlit continue to run.
  2. 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:open
  • Created 3 years ago
  • Reactions:5
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
randyzwitchcommented, Sep 3, 2020

Thanks for the code example. What is your overall use case for using multiple threads in this manner?

0reactions
silas2471commented, Mar 30, 2021

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

App performance caching and multithreading - Using Streamlit
Hi Streamlit community, I recently made an app that displays a lot of images in two columns. Now the performance is not as...
Read more >
Issue with threading: text not displayed - Using Streamlit
I just can't use the event loop directly. ... where Streamlit itself is running multiple threads, so maybe the context gets lost somewhere....
Read more >
Optimize performance with st.cache - Streamlit Docs
Example 2: When the function arguments change. Without stopping the previous app server, let's change one of the arguments to our cached function:....
Read more >
How to run a subprocess programs using thread inside ...
App performance caching and multithreading. Need help with threading in function (ibapi). STqdm : a tqdm-like progress bar for streamlit.
Read more >
Multithreading - Using Streamlit
I want to enable multi threading so that the training takes place on the secondary thread and the main thread continues.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found