How to apply threading lock in streamlit?

Hi, I write an app which uses the ECMWF Magics (https://anaconda.org/conda-forge/magics) to produce weather analysis maps in streamlit. When one user accesses the app, it’s OK. But several users access the app at the same time, I always get the error: “Segmentation fault (core dumped)”. I think the ECMWF Magics doesn’t support multiple threadings. So I want to lock the threading when one user makes some pictures with Magics. I use the following code:

from threading import Lock
import streamlit as st

lock = Lock()

with lock:
    # call the Magics functions.
    image = draw_weather_analysis()
    st.image(image, use_column_width=True)

But this does not worked. The “Segmentation fault (core dumped)” error still display, and I found the ‘lock’ can not lock the streamlit thread. So how can I do? Thanks very much.

Hello @kan,

As the code is run in different threads (one per user session), in your example you are just creating two different locks in each session. What you want instead is a lock shared with every session.

To do that, you could try to use @thiago’s GlobalState.

Another option is to instead make a small webapp around magics so that becomes a single threaded API. Then your streamlit app just calls out to that, no locking and hacking needed.

1 Like

Yet another option, you could try to do your magics computation using multiprocessing.

Hi, Synode:
Thanks for your reply very much. With the global state of “st_state_patch”, I can lock the threading and the Magics functions run correctly.

from threading import Lock
import streamlit as st
import st_state_patch

s = st.GlobalState(key="mySate")
if not s:
    s.lock = Lock()

with s.lock:
    # call the Magics functions.
    image = draw_weather_analysis()
    st.image(image, use_column_width=True)
1 Like