-
-
Save tkersey/25e9540ed0866b7fc80665641f9fe116 to your computer and use it in GitHub Desktop.
Simple way to run heavy computations without slowing down other Streamlit users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import concurrent.futures # We'll do computations in separate processes! | |
import mymodule # This is where you'll do the computation | |
# Your st calls must go inside this IF block. | |
if __name__ == '__main__': | |
st.write("Starting a long computation on another process") | |
# Pick max number of concurrent processes. Depends on how heavy your computation is, and how | |
# powerful your machine is. | |
MAX_WORKERS = 50 | |
@st.cache_resource | |
def get_executor(): | |
return concurrent.futures.ProcessPoolExecutor(max_workers=MAX_WORKERS) | |
future = get_executor().submit( | |
# Function to run on a separate process. | |
mymodule.some_heavy_computation, | |
# Arguments to pass to the function above. | |
a=100, b=200, c=300) | |
# Wait for result. | |
result = future.result() | |
st.write("Here's the result of that computation:", result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment