From 5db329bb47ff774f2044de9dfa2ad4becc29d429 Mon Sep 17 00:00:00 2001 From: Debbie Matthews Date: Sat, 21 Dec 2024 21:49:43 -0800 Subject: [PATCH] Multithreading examples --- .../multithreading-expose-context.py | 38 ++++++++++++++++++ .../multithreading-no-streamlit-batched.py | 29 ++++++++++++++ .../multithreading-no-streamlit-iterative.py | 40 +++++++++++++++++++ python/concept-source/requirements.txt | 1 + 4 files changed, 108 insertions(+) create mode 100644 python/concept-source/multithreading-expose-context.py create mode 100644 python/concept-source/multithreading-no-streamlit-batched.py create mode 100644 python/concept-source/multithreading-no-streamlit-iterative.py create mode 100644 python/concept-source/requirements.txt diff --git a/python/concept-source/multithreading-expose-context.py b/python/concept-source/multithreading-expose-context.py new file mode 100644 index 000000000..e69b56b7e --- /dev/null +++ b/python/concept-source/multithreading-expose-context.py @@ -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") diff --git a/python/concept-source/multithreading-no-streamlit-batched.py b/python/concept-source/multithreading-no-streamlit-batched.py new file mode 100644 index 000000000..0d82c2c42 --- /dev/null +++ b/python/concept-source/multithreading-no-streamlit-batched.py @@ -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") diff --git a/python/concept-source/multithreading-no-streamlit-iterative.py b/python/concept-source/multithreading-no-streamlit-iterative.py new file mode 100644 index 000000000..dc4890260 --- /dev/null +++ b/python/concept-source/multithreading-no-streamlit-iterative.py @@ -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") diff --git a/python/concept-source/requirements.txt b/python/concept-source/requirements.txt new file mode 100644 index 000000000..a649ba760 --- /dev/null +++ b/python/concept-source/requirements.txt @@ -0,0 +1 @@ +streamlit>=1.41.0 \ No newline at end of file