[Tutor] setting a timer

eryksun eryksun at gmail.com
Wed Sep 12 21:10:57 CEST 2012


On Wed, Sep 12, 2012 at 1:56 PM, Matthew Ngaha <chigga101 at gmail.com> wrote:
> i have a way to set a timer for creating new objects copied from a
> book but as i have started my next exercise in a very different way, i
> want to avoid that math method/formula as it will cause me to
> rearrange some classes totally...
>
> i tried a simple method but its not working. any ideas?

threading has a Timer:

http://docs.python.org/library/threading#timer-objects

    >>> def print_msg(msg):
    ...     print msg

    >>> t = threading.Timer(1, print_msg, ('Spam',))
    >>> t.start()
    >>> Spam

    >>> t = threading.Timer(3, print_msg, ('Spam',))
    >>> t.start(); t.join()  # waiting...
    Spam

There's also the sched module, but only if you're not using threads:

http://docs.python.org/library/sched

    >>> s = sched.scheduler(time.time, time.sleep)

    >>> e = s.enter(3, 1, print_msg, ('Spam',))
    >>> s.run()
    Spam

    >>> e = s.enterabs(time.time() + 5, 1, print_msg, ('Spam',))
    >>> s.run()
    Spam


More information about the Tutor mailing list