Skip to content

Commit

Permalink
Multithreading examples (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dmatthews authored Dec 22, 2024
1 parent 32dacd6 commit be81137
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
38 changes: 38 additions & 0 deletions python/concept-source/multithreading-expose-context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import streamlit as st
from streamlit.runtime.scriptrunner import add_script_run_ctx, get_script_run_ctx
import time
from threading import Thread


class WorkerThread(Thread):
def __init__(self, delay, target):
super().__init__()
self.delay = delay
self.target = target

def run(self):
# runs in custom thread, but can call Streamlit APIs
start_time = time.time()
time.sleep(self.delay)
end_time = time.time()
self.target.write(f"start: {start_time}, end: {end_time}")


delays = [5, 4, 3, 2, 1]
result_containers = []
for i, delay in enumerate(delays):
st.header(f"Thread {i}")
result_containers.append(st.container())

threads = [
WorkerThread(delay, container)
for delay, container in zip(delays, result_containers)
]
for thread in threads:
add_script_run_ctx(thread, get_script_run_ctx())
thread.start()

for thread in threads:
thread.join()

st.button("Rerun")
29 changes: 29 additions & 0 deletions python/concept-source/multithreading-no-streamlit-batched.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import streamlit as st
import time
from threading import Thread


class WorkerThread(Thread):
def __init__(self, delay):
super().__init__()
self.delay = delay
self.return_value = None

def run(self):
start_time = time.time()
time.sleep(self.delay)
end_time = time.time()
self.return_value = f"start: {start_time}, end: {end_time}"


delays = [5, 4, 3, 2, 1]
threads = [WorkerThread(delay) for delay in delays]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for i, thread in enumerate(threads):
st.header(f"Thread {i}")
st.write(thread.return_value)

st.button("Rerun")
40 changes: 40 additions & 0 deletions python/concept-source/multithreading-no-streamlit-iterative.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import streamlit as st
import time
from threading import Thread


class WorkerThread(Thread):
def __init__(self, delay):
super().__init__()
self.delay = delay
self.return_value = None

def run(self):
start_time = time.time()
time.sleep(self.delay)
end_time = time.time()
self.return_value = f"start: {start_time}, end: {end_time}"


delays = [5, 4, 3, 2, 1]
result_containers = []
for i, delay in enumerate(delays):
st.header(f"Thread {i}")
result_containers.append(st.container())

threads = [WorkerThread(delay) for delay in delays]
for thread in threads:
thread.start()
thread_lives = [True] * len(threads)

while any(thread_lives):
for i, thread in enumerate(threads):
if thread_lives[i] and not thread.is_alive():
result_containers[i].write(thread.return_value)
thread_lives[i] = False
time.sleep(0.5)

for thread in threads:
thread.join()

st.button("Rerun")
1 change: 1 addition & 0 deletions python/concept-source/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
streamlit>=1.41.0

0 comments on commit be81137

Please sign in to comment.