S
Solara10mo ago
romero61

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
MaartenBreddels
MaartenBreddels10mo ago
Hi! What do you mean by crashing ? And can we see a simplified version of the code?
romero61
romero6110mo ago
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)
MaartenBreddels
MaartenBreddels10mo ago
I've modified it such that it runs, but it doesn't stop the server
MaartenBreddels
MaartenBreddels10mo ago
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
romero61
romero618mo ago
@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.