Background workers not stopping

import streamlit as st
import time

class Task:
    def __init__(self):
        pass

    def run(self):
        for i in range(100):
            print(i)
            time.sleep(1)

if st.button('run some long task'):
    t = Task()
    for i in t.run():
        print(i)

if you run the above app with streamlit run main.py, and immediately click on the button a couple of times multiple instances of that task will start. but, the task is not being freed when you stop or even close the session. I am assuming for every button press a new background worker is being started?, but these background workers are not stopping after pressing stop or session disconnect.

Can you provide a solution on how to stop the background workers taking up resources, even though there is no user connection.

1 Like