Thread 'ThreadPoolExecutor-8_0': missing ScriptRunContext

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’

Debug info

  • Streamlit version: 1.12.0
  • Python version: 3.9.13
  • PipEnv

The add_script_run_ctx() accepts a thread not an iterator from a map.

Try this.

with concurrent.futures.ThreadPoolExecutor() as executor:
    executor.map()

    for t in executor._threads:
        add_script_run_ctx(t)

I modified my code this way, the error still seems to persist: Thread ‘ThreadPoolExecutor-4_0’: missing ScriptRunContext

with concurrent.futures.ThreadPoolExecutor() as executor:

        executor.map(lambda f: ingestion(*f), final)

        for t in executor._threads:
            add_script_run_ctx(t)

Try this simple example. It worked in my test.

Code

"""
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}')

Output

image

1 Like

Hi @Sanika_Sawant I encountered the same error. Have you figured out the solution?