-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32dacd6
commit be81137
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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
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
29
python/concept-source/multithreading-no-streamlit-batched.py
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
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
40
python/concept-source/multithreading-no-streamlit-iterative.py
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
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") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
streamlit>=1.41.0 |