Timers in Python?

Mike Fletcher mfletch at tpresence.com
Mon Dec 18 14:55:02 EST 2000


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

Python doesn't automatically call the superclass __init__, you need to do it
explicitly if you want it called.

HTH,
Mike

-----Original Message-----
From: Daniel.Kinnaer at AdValvas.be [mailto:Daniel.Kinnaer at AdValvas.be]
Sent: Monday, December 18, 2000 2:18 PM
To: python-list at python.org
Subject: Re: Timers in Python?


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
-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list