/Doc/library/sched.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 133 lines · 96 code · 37 blank · 0 comment · 0 complexity · 415ed955347665198888734957895796 MD5 · raw file

  1. :mod:`sched` --- Event scheduler
  2. ================================
  3. .. module:: sched
  4. :synopsis: General purpose event scheduler.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. .. index:: single: event scheduling
  7. The :mod:`sched` module defines a class which implements a general purpose event
  8. scheduler:
  9. .. class:: scheduler(timefunc, delayfunc)
  10. The :class:`scheduler` class defines a generic interface to scheduling events.
  11. It needs two functions to actually deal with the "outside world" --- *timefunc*
  12. should be callable without arguments, and return a number (the "time", in any
  13. units whatsoever). The *delayfunc* function should be callable with one
  14. argument, compatible with the output of *timefunc*, and should delay that many
  15. time units. *delayfunc* will also be called with the argument ``0`` after each
  16. event is run to allow other threads an opportunity to run in multi-threaded
  17. applications.
  18. Example::
  19. >>> import sched, time
  20. >>> s = sched.scheduler(time.time, time.sleep)
  21. >>> def print_time(): print "From print_time", time.time()
  22. ...
  23. >>> def print_some_times():
  24. ... print time.time()
  25. ... s.enter(5, 1, print_time, ())
  26. ... s.enter(10, 1, print_time, ())
  27. ... s.run()
  28. ... print time.time()
  29. ...
  30. >>> print_some_times()
  31. 930343690.257
  32. From print_time 930343695.274
  33. From print_time 930343700.273
  34. 930343700.276
  35. In multi-threaded environments, the :class:`scheduler` class has limitations
  36. with respect to thread-safety, inability to insert a new task before
  37. the one currently pending in a running scheduler, and holding up the main
  38. thread until the event queue is empty. Instead, the preferred approach
  39. is to use the :class:`threading.Timer` class instead.
  40. Example::
  41. >>> import time
  42. >>> from threading import Timer
  43. >>> def print_time():
  44. ... print "From print_time", time.time()
  45. ...
  46. >>> def print_some_times():
  47. ... print time.time()
  48. ... Timer(5, print_time, ()).start()
  49. ... Timer(10, print_time, ()).start()
  50. ... time.sleep(11) # sleep while time-delay events execute
  51. ... print time.time()
  52. ...
  53. >>> print_some_times()
  54. 930343690.257
  55. From print_time 930343695.274
  56. From print_time 930343700.273
  57. 930343701.301
  58. .. _scheduler-objects:
  59. Scheduler Objects
  60. -----------------
  61. :class:`scheduler` instances have the following methods and attributes:
  62. .. method:: scheduler.enterabs(time, priority, action, argument)
  63. Schedule a new event. The *time* argument should be a numeric type compatible
  64. with the return value of the *timefunc* function passed to the constructor.
  65. Events scheduled for the same *time* will be executed in the order of their
  66. *priority*.
  67. Executing the event means executing ``action(*argument)``. *argument* must be a
  68. sequence holding the parameters for *action*.
  69. Return value is an event which may be used for later cancellation of the event
  70. (see :meth:`cancel`).
  71. .. method:: scheduler.enter(delay, priority, action, argument)
  72. Schedule an event for *delay* more time units. Other then the relative time, the
  73. other arguments, the effect and the return value are the same as those for
  74. :meth:`enterabs`.
  75. .. method:: scheduler.cancel(event)
  76. Remove the event from the queue. If *event* is not an event currently in the
  77. queue, this method will raise a :exc:`ValueError`.
  78. .. method:: scheduler.empty()
  79. Return true if the event queue is empty.
  80. .. method:: scheduler.run()
  81. Run all scheduled events. This function will wait (using the :func:`delayfunc`
  82. function passed to the constructor) for the next event, then execute it and so
  83. on until there are no more scheduled events.
  84. Either *action* or *delayfunc* can raise an exception. In either case, the
  85. scheduler will maintain a consistent state and propagate the exception. If an
  86. exception is raised by *action*, the event will not be attempted in future calls
  87. to :meth:`run`.
  88. If a sequence of events takes longer to run than the time available before the
  89. next event, the scheduler will simply fall behind. No events will be dropped;
  90. the calling code is responsible for canceling events which are no longer
  91. pertinent.
  92. .. attribute:: scheduler.queue
  93. Read-only attribute returning a list of upcoming events in the order they
  94. will be run. Each event is shown as a :term:`named tuple` with the
  95. following fields: time, priority, action, argument.
  96. .. versionadded:: 2.6