Skip to content

Commit be81137

Browse files
Multithreading examples (#1210)
1 parent 32dacd6 commit be81137

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import streamlit as st
2+
from streamlit.runtime.scriptrunner import add_script_run_ctx, get_script_run_ctx
3+
import time
4+
from threading import Thread
5+
6+
7+
class WorkerThread(Thread):
8+
def __init__(self, delay, target):
9+
super().__init__()
10+
self.delay = delay
11+
self.target = target
12+
13+
def run(self):
14+
# runs in custom thread, but can call Streamlit APIs
15+
start_time = time.time()
16+
time.sleep(self.delay)
17+
end_time = time.time()
18+
self.target.write(f"start: {start_time}, end: {end_time}")
19+
20+
21+
delays = [5, 4, 3, 2, 1]
22+
result_containers = []
23+
for i, delay in enumerate(delays):
24+
st.header(f"Thread {i}")
25+
result_containers.append(st.container())
26+
27+
threads = [
28+
WorkerThread(delay, container)
29+
for delay, container in zip(delays, result_containers)
30+
]
31+
for thread in threads:
32+
add_script_run_ctx(thread, get_script_run_ctx())
33+
thread.start()
34+
35+
for thread in threads:
36+
thread.join()
37+
38+
st.button("Rerun")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import streamlit as st
2+
import time
3+
from threading import Thread
4+
5+
6+
class WorkerThread(Thread):
7+
def __init__(self, delay):
8+
super().__init__()
9+
self.delay = delay
10+
self.return_value = None
11+
12+
def run(self):
13+
start_time = time.time()
14+
time.sleep(self.delay)
15+
end_time = time.time()
16+
self.return_value = f"start: {start_time}, end: {end_time}"
17+
18+
19+
delays = [5, 4, 3, 2, 1]
20+
threads = [WorkerThread(delay) for delay in delays]
21+
for thread in threads:
22+
thread.start()
23+
for thread in threads:
24+
thread.join()
25+
for i, thread in enumerate(threads):
26+
st.header(f"Thread {i}")
27+
st.write(thread.return_value)
28+
29+
st.button("Rerun")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import streamlit as st
2+
import time
3+
from threading import Thread
4+
5+
6+
class WorkerThread(Thread):
7+
def __init__(self, delay):
8+
super().__init__()
9+
self.delay = delay
10+
self.return_value = None
11+
12+
def run(self):
13+
start_time = time.time()
14+
time.sleep(self.delay)
15+
end_time = time.time()
16+
self.return_value = f"start: {start_time}, end: {end_time}"
17+
18+
19+
delays = [5, 4, 3, 2, 1]
20+
result_containers = []
21+
for i, delay in enumerate(delays):
22+
st.header(f"Thread {i}")
23+
result_containers.append(st.container())
24+
25+
threads = [WorkerThread(delay) for delay in delays]
26+
for thread in threads:
27+
thread.start()
28+
thread_lives = [True] * len(threads)
29+
30+
while any(thread_lives):
31+
for i, thread in enumerate(threads):
32+
if thread_lives[i] and not thread.is_alive():
33+
result_containers[i].write(thread.return_value)
34+
thread_lives[i] = False
35+
time.sleep(0.5)
36+
37+
for thread in threads:
38+
thread.join()
39+
40+
st.button("Rerun")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
streamlit>=1.41.0

0 commit comments

Comments
 (0)