I am having again problems with the hidden async/multithreading nature of streamlit. I just started to work on a new local app that interfaces a hardware component (power supply). In the example below it is absolutely necessary that the dps functions are atomic and do not mix in their execution. The while loop runs fine. However, if I press the button, the dps.reset_statistics() function causes errors. By changing the sleep time in the while loop I can verify that this only happens if press the button when the dps.output() function is about to execute. Reducing the sleep time, causes the errors to appear always. This tells me that streamlit interrupts the button triggered function by the dps.output() function which then causes the communication with the instrument to fail. Any ideas what to do? This is with version 1.42. I did similar things in the past but dont remember this kind of problems. I am using this simple modbus script that does not do any threading/async things: minimalmodbus/minimalmodbus.py at master · pyhys/minimalmodbus · GitHub
import streamlit as st
import sk120
import time
session = st.session_state
if 'init' not in session: # init section
session.init = True
@st.cache_resource
def init():
ser = sk120.Serial_modbus('/dev/ttyUSB1', 1, 115200, 8)
dps = sk120.sk120(ser)
return dps
dps = init()
if st.button('reset statistics'):
dps.reset_statistics()
pos1 = st.empty()
while True :
pos1.write(dps.output())
time.sleep(2)