Tags: certain, code, events, learn, programming, python, regarding, time, timed, windows, xpi

Timed events in Python

On Programmer » Python

6,432 words with 1 Comments; publish: Fri, 04 Jan 2008 14:42:00 GMT; (200140.63, « »)

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: ( text )
  1. >>> import sched, time
  2. >>> s=sched.scheduler(time.time, time.sleep)
  3. >>> def print_time(): print "From print_time", time.time()
  4. ...
  5. >>> def print_some_times():
  6. ... print time.time()
  7. ... s.enter(5, 1, print_time, ())
  8. ... s.enter(10, 1, print_time, ())
  9. ... s.run()
  10. ... print time.time()
  11. ...
  12. >>> print_some_times()

Questions:

1. whenever I do a scheduled event should I have this line?

s=sched.scheduler(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

All Comments

Leave a comment...

  • 1 Comments
    • Quote:
      === Original Words ===

      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: ( text )
      1. >>> import sched, time
      2. >>> s=sched.scheduler(time.time, time.sleep)
      3. >>> def print_time(): print "From print_time", time.time()
      4. ...
      5. >>> def print_some_times():
      6. ... print time.time()
      7. ... s.enter(5, 1, print_time, ())
      8. ... s.enter(10, 1, print_time, ())
      9. ... s.run()
      10. ... print time.time()
      11. ...
      12. >>> print_some_times()

      Questions:

      1. whenever I do a scheduled event should I have this line?

      s=sched.scheduler(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.
      #1; Fri, 04 Jan 2008 14:43:00 GMT