Timers in Python?

Daniel Daniel.Kinnaer at AdValvas.be
Mon Dec 18 14:18:26 EST 2000


On Mon, 18 Dec 2000 18:46:13 +0100, "Gilles Lenfant"
<glenfant at equod.com.nospam> wrote:

>Have a look to "threading" package and time.sleep(...) function.
>Seems that's all you need!
>Important, use threading.Lock() objects if threads may use not sharable
>resources.
>Warning, binary distributions of Python/Unix are not always compiles with
>threads options.
>You may need to compile Python from sources if required.
>

Dear Gilles,
Thanks for the reply.  It seems I ran into trouble when trying the
above code.  Python replied me the following :

timers.py :

import threading, time

Mylock = threading.Lock()

class TimeScheduled(threading.Thread):
    def __init__(self, function, delay):
        self.function = function
        self.delay = delay

    def run(self):
        self.alive = 1
        while self.alive:
            Mylock.acquire()
            # optional (depends whether unsharable resources used or
not)
            self.function()
            Mylock.release() # optional
            time.sleep(self.delay)

    def kill(self):
        self.alive = 0

def sayhello():
    # Very complex stuff
    print "Hello world"

# Every 2 minutes
scheduledHello = TimeScheduled(sayhello, 120)
scheduledHello.start() #this is line28

scheduledHello.kill()



Reply by Python : 

Traceback (innermost last):
  File "C:\Python20\AProject\timers\timers.py", line 28, in ?
    scheduledHello.start()
  File "c:\python20\lib\threading.py", line 351, in start
    assert self.__initialized, "Thread.__init__() not called"
AssertionError: Thread.__init__() not called

Is there something that can be done about it?  Thanks

Daniel



More information about the Python-list mailing list