Streamlit session_state with multiprocesssing

Is there any recommended solution for using streamlit session_states while running a part of your code with multiprocessing.Pool?

Of course, the problem is that by using multiprocessing, we run different processes (instead of the main streamlit run …) and the new processes don’t inherit the session_states, resulting in KeyError. I’m curious to know whether there’s a workaround or it’s a limitation that we have to deal with it (i.e. either using session_state or multiprocessing, and not both).

Thanks

1 Like

Sharing data between processes can be tricky. There are several ways of doing it documented in the multiprocessing module. The recommended solution would depend on the specifics of your use case.

Thanks for your reply @Goyo , but that’s not what I was looking for.

Indeed, sharing the states between the processes are tricky. But the question is if streamlit supports using of multiprocessing and session_state together?

You should be able to use both multiprocessing and session_state in a streamlit application. The workaround for “new processes don’t inherit the session_states” is sharing the data in some other way. I can’t be more specific without knowing more about your use case.