#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Stopping a threading.timer -


    Hi,

    In the below function, when I change the global variable timeron to 'no', I receive the following error:
    ...in printit
    t.cancel()
    UnboundLocalError: local variable 't' referenced before assignment


    def printit():
    global timez
    global timeron
    if timeron == 'yes':
    t = threading.Timer(2.0, printit).start()
    if timeron == 'no':
    t.cancel()
    timeron = 'unknown'
    now = datetime.datetime.now()
    timez = "Second: %d" % now.second
    print "Second: %d" % now.second


    Not sure why it doesn't know that by t I am referring to t = threading.Timer(2.0, printit)

    Thanks for the help
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    FYI, the forum software here does not retain indentation by default. To show your code with the correct indentation, you need to put the code inside of [CODE] tags (or [HIGHLIGHT] tags, either one).

    The main issue you are having is that the variable t is initialized inside of an if: block, which has the result of making it local to that block. You need to initialize t to some neutral value (such as None) before the if: statement, so that it is in scope of both if: statements.

    Code:
    def printit():
        global timez
        global timeron
       
        t = None
        if timeron == 'yes':
            t = threading.Timer(2.0, printit).start()
        if timeron == 'no':
            t.cancel()
            timeron = 'unknown'
        now = datetime.datetime.now()
        timez = "Second: %d" % now.second
        print "Second: %d" % now.second
    Last edited by Schol-R-LEA; November 8th, 2012 at 07:53 AM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    Code:
    if False:
        t = 0
    else:
        print(t)
    Run this program in a new session of the python interpreter.
    Use the debugger.
    Ask all your programmer friends.
    Think about it with all your might and glory.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0
    edit: sorry never mind - figured it out myself for one

IMN logo majestic logo threadwatch logo seochat tools logo