Timed events in Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    #1

    Timed events in Python

    Python 2.5 in Windows XP

    I'm trying to learn about getting one's code to work at a certain time. I have some questions regarding what i've read from http://www.python.org/doc/current/lib/module-sched.html. If anyone could help that'd be great. Below is the sample code provided from the link above:

    Code:
     >>> import sched, time
    >>> s=sched.scheduler(time.time, time.sleep)
    >>> def print_time(): print "From print_time", time.time()
    ...
    >>> def print_some_times():
    ...     print time.time()
    ...     s.enter(5, 1, print_time, ())
    ...     s.enter(10, 1, print_time, ())
    ...     s.run()
    ...     print time.time()
    ...
    >>> print_some_times()
    Questions:
    1. whenever I do a scheduled event should I have this line?
    s=sched.schedul er(time.time, time.sleep)

    2. In...s.enter(5, 1, print_time, ())
    What does the 5 and 1 represent? Possibly how long to wait before doing the function and how long it lasts?

    That's all for now, i'm sure I"ll have follow ups.
    Thanks
    PC
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Patrick C
    Python 2.5 in Windows XP

    I'm trying to learn about getting one's code to work at a certain time. I have some questions regarding what i've read from http://www.python.org/doc/current/lib/module-sched.html. If anyone could help that'd be great. Below is the sample code provided from the link above:

    Code:
     >>> import sched, time
    >>> s=sched.scheduler(time.time, time.sleep)
    >>> def print_time(): print "From print_time", time.time()
    ...
    >>> def print_some_times():
    ...     print time.time()
    ...     s.enter(5, 1, print_time, ())
    ...     s.enter(10, 1, print_time, ())
    ...     s.run()
    ...     print time.time()
    ...
    >>> print_some_times()
    Questions:
    1. whenever I do a scheduled event should I have this line?
    s=sched.schedul er(time.time, time.sleep)

    2. In...s.enter(5, 1, print_time, ())
    What does the 5 and 1 represent? Possibly how long to wait before doing the function and how long it lasts?

    That's all for now, i'm sure I"ll have follow ups.
    Thanks
    PC
    To schedule events using the sched module, create an instance of the scheduler class, which you have done. '5' is the delay from the time the enter() method is called, and '1' is the priority. See this doc page for more info.

    Comment

    Working...