I am trying to use python concurrent.futures.ThreadPoolExecutor() to run multiple instances of âingestionâ function for given metadata.
âfinalâ is a list of tuples that holds the values of arguments for the function called.
As per the existing discussions, I installed streamlit==1.12.0 to use add_script_run_ctx()
Original python script without streamlit dependency (was working fine):
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(lambda f: ingestion(*f), final)
Code snippet:
from streamlit.runtime.scriptrunner import add_script_run_ctx
with concurrent.futures.ThreadPoolExecutor() as executor:
abc = executor.map(lambda f: ingestion(*f), final)
add_script_run_ctx(abc)
Expected behavior:
The code should enter the âingestionâ function and iterate through tuples present in the âfinalâ list.
Actual behavior:
Thread âThreadPoolExecutor-8_0â: missing ScriptRunContext
âgeneratorâ object has no attribute âstreamlit_script_run_ctxâ
"""
streamlit==1.12.0
"""
import time
import concurrent.futures
import streamlit as st
from streamlit.runtime.scriptrunner import add_script_run_ctx
final = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
def multip(a, b):
time.sleep(a)
return a * b
if __name__ == '__main__':
if st.button('run'):
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
results = executor.map(lambda f: multip(*f), final)
for t in executor._threads:
add_script_run_ctx(t)
res = []
for value in results:
res.append(value)
st.write(f'res: {res}')