Generator already executing

I have tinkered around different mechanisms to fix this recurring issue. However I keep coming to the same issue. in different ways.

Top level I have a stream of data and it periodically creates snapshots via buttons to navigate to. The aim is to be able to click the buttons to navigate to snapshot information however in the meantime it will be giving analytics of the current snapshot which is a stream of data.

However, I get the following error.

Exception has occurred: ValueError       (note: full exception trace is shown but execution is paused at: <module>)
generator already executing

For the following code.

@st.cache_resource
def get_stream():
    return get_data_stream(topic_name=Settings().KAFKA_TOPIC)

def process_data_stream():
    for tx in get_stream():
        if st.session_state.current_view == "pending_block":
            current_block_number = process_transaction(transaction=tx)
            block_data_to_display = get_block_data(block_number=st.session_state.current_view)
            current_metrics_for_block = block_data_to_display.metrics
            update_display(current_metrics_for_block)
            if current_block_number is not None:
                update_side_bar(container=historical_holder)
        else:
            snapshot_data = get_block_data(block_number=st.session_state.current_view)
            update_display(snapshot_data.metrics)
        
        time.sleep(0.01)  # Small delay to prevent blocking

if __name__ == "__main__":
    process_data_stream()

The above is a stripped-down version of the code. I had a bunch of try catches, but they were not useful and async functionalities just in case.

I understand when I click a widget it reloads the page. This is why I am using cache resource; however, I assume it is trying to create another generator object or the iteration of the generator is breaking the execution.

My understanding is since the resource is cached it shouldn’t recreate the data stream.

I can’t see the stacktrace.

When streamlit component was pressed this will execute again thus getting the following aforementioned error.

if __name__ == "__main__":
    process_data_stream()

However I know get the following error.

Exception has occurred: StopException       (note: full exception trace is shown but execution is paused at: <module>)
exception: no description
  File "/consumer/Latest_block.py", line 112, in process_data_stream
    if st.session_state.current_view == "pending_block":
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/consumer/Latest_block.py", line 129, in <module> (Current frame)
    process_data_stream()
streamlit.runtime.scriptrunner_utils.exceptions.StopException: