Solved by albanD in post #2 
              
              
                For the global flag to work, you need to define it as global as you modify it: 
global flag
if flag:
  flag = False
  # Do something