Oct-01-2020, 04:16 AM
Thanks for all responses ! It is not fully clear to me yet. I will try to explain best to my ability my recent findings:
The issue is that I want to be able to stop current task and start a new one even when I am in the middle of a current task.
Assume that I have currently started a task and this function "handle_next_pick" is being called every 1 second.
When I press the button on my GUI, it executed this function:
My recent findings are:
Instead of doing everyting when the button is pressed, I just tried to set the global flag:
Could someone help me understand this behaviour?
def handle_next_pick(): print("handling next pick") global item_number global flag_next_device print("pick counter =",pick_counter) if(pick_counter < Counter_global): ## THIS IS ALWAYS TRUE WHEN THE OPERATION STARTS if(flag_next_device>0): //FLAG_NEXT_DEVICE IS SET BY TRIGGERING THE REMOTE SENSOR. print("flag_next_device is higher than 1") if(item_number > 0):#only check if it divides by counter if its higher then 0 to avoid glitch if(item_number % Counter_global_temp==0): item_number=item_number-(Counter_global_temp) app.update_rectangle_color(Device_global_temp[item_number]) string = Device_global_temp[item_number]+"/"+"status" mqttc.publish(string,"ACTIVE",1) flag_next_device=0 if(update_DB==1): update_DB_Quantity(myConnection) else: print("Dont need to update DB") app.after(100,handle_next_pick) else: app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND print("wait for next command") else: app.after(1000,handle_end_of_picking)So the code below is my main "task" code. I have addded some additional comments to explain what is going on. At the bottom of the function, I recursively call :
app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND print("wait for next command")This just checks whether the flag has been set every 1 second. And it continues this loop untill I complete the task.
The issue is that I want to be able to stop current task and start a new one even when I am in the middle of a current task.
Assume that I have currently started a task and this function "handle_next_pick" is being called every 1 second.
When I press the button on my GUI, it executed this function:
def New_operation(self): print("new_operation") restart_devices(myConnection,"reset") canvas.delete("all") app.after(1000,self.Application_Intro) print("new operation end")So I obviously expect this function to clear all canvas, interrupt the current task and start a new one, however, thats not the case! After clearing canvas and displaying a new GUI , it continues to call handle_next_pick function every 1 second which is what I am trying to solve.
My recent findings are:
Instead of doing everyting when the button is pressed, I just tried to set the global flag:
def New_operation(self): global reset reset = 1 # this is defined as global variable therefore will be recognised outsideAnd then In my task code, I am constantly checking whether this flag is set:
app.after(1000,handle_next_pick)#IF THE SENSOR IS NOT SET, CHECK AGAIN AFTER 1 SECOND print("wait for next command") # check if the flag is set to restart the program if (reset==1): reset = 0 canvas.delete("all") app.after(1000,self.Application_Intro) return # IF RETURN USED HERE, PROGRAM RESETS AND WORKS AS EXPECTNotice that return I have added after Calling Application_Intro. This return makes the program reset properly and start from the beggining. If I do not use return, It continues to call handle_next_pick every 1 second.
Could someone help me understand this behaviour?