Use_thread
Hello,
I've been using a separate thread to run a long computation process in my application using solara.use_thread. The idea is to keep the UI responsive and prevent server disconnects. I've attached a simplified flow diagram of my setup.
However, I'm experiencing inconsistent behavior. Sometimes the process runs perfectly for a long time, but at other times, it crashes almost immediately or after a short while.
Here's the flow:
+------------------+ +-----------------+
| ModelControl | --> | thread_function |
| (Start/Stop Btn) | | (Model Logic) |
+------------------+ +-----------------+
| |
V V
+------------------+ +-----------------+
| solara.thread | | CNN Execution |
| (Separate Thread)| | (Model Logic) |
+------------------+ +-----------------+
|
V
+------------------+
| StatusBar |
| (Display Status) |
+------------------+
5 Replies
Hi! What do you mean by crashing ? And can we see a simplified version of the code?
Sorry what I mean by crash is I get a server disconnected that looks like the solar.dev server disconnected notification.
running = solara.reactive(False)
status_message = solara.reactive("Ready to start.")
timer_value = solara.reactive(0)
start_time = solara.reactive(None)
elapsed_time = solara.reactive(None)
model_status = solara.reactive('Waiting')
thread_running = threading.Event()
@solara.component
def ModelControl(running, status_message, start_time):
def start_model():
running.value = True
start_time.value = datetime.datetime.now()
thread_running.set()
def stop_model():
thread_running.clear()
running.value = False
status_message.value = "Model stopped by user."
if running.value:
return solara.Button(label="Stop Execution", on_click=stop_model)
else:
return solara.Button(label="Start Model", on_click=start_model)
@solara.component
def CombinedThread(running, timer_value,t1, elapsed_time, status_message, available_dates, selected_coordinates, selected_country, model_status):
def thread_function():
while True:
thread_running.wait() # Wait until signaled to start.
if available_dates.value and running.value:
status_message.value = "Model is running..."
timer_value.value +=1
final_df = CNN(model_status, t1, elapsed_time,
dates_list=available_dates.value,
coordinates=selected_coordinates.value,
country=selected_country.value )
if model_status.value == "Model run complete!":
status_message.value = "Model run complete!"
timer_value.value -=1
available_dates.value = None
thread_running.clear()# Signal the thread to stop.
running.value = False
elif not available_dates.value and status_message.value != 'Model run complete!':
status_message.value = 'Requires Date Selection'
running.value = False
timer_value.value = 0
thread_running.clear()
result = solara.use_thread(thread_function, dependencies=[running.value])
@solara.component
def Page():
ModelControl(running, status_message, start_time)
CombinedThread(running, timer_value, elapsed_time, status_message, model_status)
I've modified it such that it runs, but it doesn't stop the server
do you have an error message why it stops the server?
also, i'm not sure your logic is correct, and I don't think you need the threading.Event
@maartenbreddels I wanted to thank you for your response to this, I ended up getting fulltime employment on top of the internship I am building this for so I got quite busy. It still has this bug but to clarify its on huggingface and runs perfectly fine locally. I can't seem to figure what the issue is there so I may just to have to live with it. I'll be sharing it shortly on the showcase channel.