Joel Auterson
Joel Auterson

Reputation: 728

Python Timers not working

I have the following code:

        def countdown():
            def countdown1():
                print 'countdown1'
                def countdown2():
                    def countdown3():
                        takePic()
                    self.pic.set_markup("<span size='54000'>1</span>");
                    print 1
                    t3 = Timer(1.0, countdown3)
                    t3.start()
                self.pic.set_markup("<span size='54000'>2</span>");
                print 2
                t2 = Timer(1.0, countdown2)
                t2.start()
            self.pic.set_markup("<span size='54000'>3</span>");
            print 3
            t1 = Timer(1.0, countdown1)
            t1.start()

        countdown()

It should show a countdown from 3. The number 3 appears, but afterwards nothing happens. help?

Upvotes: 0

Views: 766

Answers (3)

Amber
Amber

Reputation: 526533

Why not just .join() your timer threads after you .start() them, so that the rest of your code waits until the timers are done to continue?

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

Your main thread is probably exiting before any timers fire. The simplest and crudest way to fix this is to get the main thread to sleep for as long as necessary. A saner option is to signal something like a semaphore at the end of countdown3 and wait on it in the main thread.

A more elegant solution, which can be integrated with a broader scheduling and asynchrony framework, is to invert the flow of control using generators:

def countdown():
    self.pic.set_markup("<span size='54000'>3</span>");
    print 3
    yield 1.0

    print 'countdown1'
    self.pic.set_markup("<span size='54000'>2</span>");
    print 2
    yield 1.0

    self.pic.set_markup("<span size='54000'>1</span>");
    print 1
    yield 1.0

    takePic()

for t in countdown():
    time.sleep(t)

Upvotes: 2

Daniel DiPaolo
Daniel DiPaolo

Reputation: 56390

Are you sure some other command isn't blocking? Like set_markup? A simplified example works for me:

>>> from threading import Timer
>>> def lvl1():
    def lvl2():
        print "evaling lvl2"
        def lvl3():
            print "evaling lvl3"
            print "TakePic()"
        print 1
        t3 = Timer(1.0, lvl3)
        t3.start()
    print 2
    t2 = Timer(2.0, lvl2)
    t2.start()

>>> lvl1()
2
>>> evaling lvl2
1
evaling lvl3
TakePic()

Upvotes: 0

Related Questions