cancellable timed loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Elfers
    New Member
    • Aug 2010
    • 9

    cancellable timed loop

    I am very new to python and programming in general and am trying to build a loop that can be cancelled when the timer ends or cancelled by a variable that will be set externally. I am testing with my code below and it doesn't want to cancel, like it never re-reads the variable to realize that it is no longer a 1. It seems like I am either really close or way off.

    Code:
    import threading
    scalavar = 1
    enable = 1
    def disable():
        scalavar=0
        print 'timer stop'
    timer = threading.Timer(5,disable)
    timer.start()
    while (enable == 1):
        if (enable == 1):
            print 'working'
            enable=scalavar
        elif (enable != 1):
            break
        
            
    print 'Goodbye'
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Declare scalavar a global variable.

    Code:
    def disable():
        global scalavar
        scalavar=0
        print 'timer stop'

    Comment

    • Dave Elfers
      New Member
      • Aug 2010
      • 9

      #3
      Thank you sir, that did the trick!

      Comment

      Working...