Last active
January 31, 2025 17:23
-
-
Save tvst/919abd4a1c229437be91aae80f08437c to your computer and use it in GitHub Desktop.
Simple way to run heavy computations without slowing down other Streamlit users
This file contains 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) |
Also: keep in mind Numpy can get absurdly slower when running in multiple processes on the same machine.
If you notice this, you should wrap you Numpy code in this with
block:
import threadpoolctl
with threadpoolctl.threadpool_limits(limits=1, user_api='blas'):
# Numpy code goes here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same thing but with Ray, so you can easily run tasks on separate machines:
Then in
mymodule.py
you do this: