Speed up your app with parallel fragments

Streamlit lets you turn functions into fragments, which can rerun independently from the full script. By default, fragments run one after another on the main thread. If your app has several slow, independent operations—like database queries or API calls—you can run them at the same time by setting parallel=True in the @st.fragment decorator. During a full-app rerun, Streamlit dispatches each parallel fragment to a thread pool so they execute concurrently instead of blocking each other.

  • Use parallel=True to run slow, independent fragments concurrently.
  • Store each fragment's results in Session State to safely combine them.
  • This tutorial requires the following version of Streamlit:

    Text
    streamlit>=1.58.0
    
  • You should have a clean working directory called your-repository.

  • You should have a basic understanding of fragments and Session State.

In this example, you'll build an app that loads data from three independent, slow sources. Without parallel fragments, the app waits for each source in turn, so the total load time is the sum of all three. By marking each source's fragment with parallel=True, the three loads run concurrently during a full-app rerun, and the app finishes in about the time of the slowest single source.

You'll simulate slow work with time.sleep, but in a real app these fragments would run database queries, call external APIs, or perform other independent, time-consuming work.

Here's a look at what you'll build:

Complete codeexpand_more
Python
import streamlit as st
import time
import random


def slow_load(source_name, seconds):
    """Simulate a slow, independent data source."""
    time.sleep(seconds)
    return {"source": source_name, "value": random.randint(0, 100)}


st.title("Parallel data loading")

@st.fragment(parallel=True)
def load_sales():
    result = slow_load("Sales", 3)
    st.session_state.sales = result
    st.metric("Sales", result["value"])

@st.fragment(parallel=True)
def load_traffic():
    result = slow_load("Traffic", 3)
    st.session_state.traffic = result
    st.metric("Traffic", result["value"])

@st.fragment(parallel=True)
def load_inventory():
    result = slow_load("Inventory", 3)
    st.session_state.inventory = result
    st.metric("Inventory", result["value"])

cols = st.columns(3)
with cols[0]:
    load_sales()
with cols[1]:
    load_traffic()
with cols[2]:
    load_inventory()
  1. In your-repository, create a file named app.py.

  2. In a terminal, change directories to your-repository, and start your app.

    Terminal
    streamlit run app.py
    

    Your app will be blank because you still need to add code.

  3. In app.py, write the following:

    Python
    import streamlit as st
    import time
    import random
    

    You'll use time to simulate slow work and random to generate sample values.

  4. Save your app.py file, and view your running app.

  5. In your app, select "Always rerun", or press the "A" key.

    Your preview will be blank but will automatically update as you save changes to app.py.

  6. Return to your code.

  7. Define a helper function to simulate a slow, independent data source.

    Python
    def slow_load(source_name, seconds):
        """Simulate a slow, independent data source."""
        time.sleep(seconds)
        return {"source": source_name, "value": random.randint(0, 100)}
    

    In a real app, you'd replace the time.sleep call with a database query, an API call, or another time-consuming operation.

  8. Add a title to your app.

    Python
    st.title("Parallel data loading")
    

Each data source is independent, so each one is a good candidate for its own parallel fragment. Each fragment writes its result to its own Session State key, which keeps the fragments from interfering with each other when they run concurrently.

  1. Define a fragment to load your first source.

    Python
    @st.fragment(parallel=True)
    def load_sales():
        result = slow_load("Sales", 3)
        st.session_state.sales = result
        st.metric("Sales", result["value"])
    

    The parallel=True argument tells Streamlit to dispatch this fragment to a thread pool during a full-app rerun so it can run at the same time as your other parallel fragments.

  2. Define fragments for your other two sources in the same way.

    Python
    @st.fragment(parallel=True)
    def load_traffic():
        result = slow_load("Traffic", 3)
        st.session_state.traffic = result
        st.metric("Traffic", result["value"])
    
    
    @st.fragment(parallel=True)
    def load_inventory():
        result = slow_load("Inventory", 3)
        st.session_state.inventory = result
        st.metric("Inventory", result["value"])
    

    Each fragment writes to a different Session State key (sales, traffic, and inventory). Because parallel fragments can run concurrently, having each one write to its own key avoids race conditions.

  1. Call each fragment in its own column.

    Python
    cols = st.columns(3)
    with cols[0]:
        load_sales()
    with cols[1]:
        load_traffic()
    with cols[2]:
        load_inventory()
    

    Each fragment renders its metric into its own column. Because the columns are created in the main body of the script, each fragment writes only into its own main body.

  2. Save your app.py file, and view your running app.

    Each fragment sleeps for three seconds, but because they run in parallel, your app loads in about three seconds total instead of nine. Try changing parallel=True to parallel=False (or removing it) to see the difference: the app will take about nine seconds because the fragments run one after another.

Replace the slow_load helper with your own slow operations, such as database queries or API calls. To learn more about parallel execution, including command restrictions and thread safety, see Run fragments in parallel.

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.