Preserving App Reactivity in a loop inside a reactive event

Hi,

I am working on a python-based shiny app that serves to drive a fluid pump over a serial transmission line. After configuring a flow profile with set amplitudes and runtime, the serial transmission can be started by pressing an action button "p1". The issue I am facing is the lack of reactivity inside reactive.event(input.p1) :
I want to ensure that the transmission started by clicking "p1" can be terminated anytime by clicking "p2". However, the current implementation causes the stop message in reactive.event(input.p2) to queue up and be sent after the transmission in reactive.event(input.p1) has ended. How can I fix this? Is there any way to preserve reactivity? Implementation of both buttons are dropped below.

    @reactive.Effect
    @reactive.event(input.p1)
    def _():
        y = yg.get() # fetch np.array y from reactive value yg, numbers in y correspond to driving voltages

         for e in y: # iterate over array
             msg = "1:1:"+str(e)+":100" # formatted string containing the driving voltage
             #print(msg) # print to console in debug mode
             ser.write(bytes(msg,'utf-8')) # send the formatted string
             t0 = time.time() # time stamp
            
             while(((time.time()-t0)<=2)): # next driving voltage should be transmitted after 2 seconds
                 pass
        ser.write(bytes("0:1",'utf-8')) # stops the pump after transmission has ended

    @reactive.Effect
    @reactive.event(input.p2)
    def _():
        #print("1:0") 
        ser.write(bytes("0:1",'utf-8')) # Stop the pump

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.