Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
44bd6c0a
Commit
44bd6c0a
authored
Jan 17, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #1861: Add read-only attribute listing upcoming events in the order they will be run.
parent
a35a8b11
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
8 deletions
+33
-8
Doc/library/sched.rst
Doc/library/sched.rst
+6
-1
Lib/sched.py
Lib/sched.py
+24
-7
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/sched.rst
View file @
44bd6c0a
...
...
@@ -47,7 +47,7 @@ Example::
Scheduler Objects
-----------------
:class:`scheduler` instances have the following methods:
:class:`scheduler` instances have the following methods
and attributes
:
.. method:: scheduler.enterabs(time, priority, action, argument)
...
...
@@ -98,3 +98,8 @@ Scheduler Objects
the calling code is responsible for canceling events which are no longer
pertinent.
.. attribute:: scheduler.queue
Read-only attribute returning a list of upcoming events in the order they
will be run. Each event is shown as a :term:`named tuple` with the
following fields: time, priority, action, argument.
Lib/sched.py
View file @
44bd6c0a
...
...
@@ -29,14 +29,17 @@ has another way to reference private data (besides global variables).
# XXX the global state of your particular time and delay functions.
import
heapq
from
collections
import
namedtuple
__all__
=
[
"scheduler"
]
Event
=
namedtuple
(
'Event'
,
'time, priority, action, argument'
)
class
scheduler
:
def
__init__
(
self
,
timefunc
,
delayfunc
):
"""Initialize a new instance, passing the time and delay
functions"""
self
.
queue
=
[]
self
.
_
queue
=
[]
self
.
timefunc
=
timefunc
self
.
delayfunc
=
delayfunc
...
...
@@ -47,8 +50,8 @@ class scheduler:
if necessary.
"""
event
=
time
,
priority
,
action
,
argument
heapq
.
heappush
(
self
.
queue
,
event
)
event
=
Event
(
time
,
priority
,
action
,
argument
)
heapq
.
heappush
(
self
.
_
queue
,
event
)
return
event
# The ID
def
enter
(
self
,
delay
,
priority
,
action
,
argument
):
...
...
@@ -67,12 +70,12 @@ class scheduler:
If the event is not in the queue, this raises RuntimeError.
"""
self
.
queue
.
remove
(
event
)
heapq
.
heapify
(
self
.
queue
)
self
.
_
queue
.
remove
(
event
)
heapq
.
heapify
(
self
.
_
queue
)
def
empty
(
self
):
"""Check whether the queue is empty."""
return
not
self
.
queue
return
not
self
.
_
queue
def
run
(
self
):
"""Execute events until the queue is empty.
...
...
@@ -97,7 +100,7 @@ class scheduler:
"""
# localize variable access to minimize overhead
# and to improve thread safety
q
=
self
.
queue
q
=
self
.
_
queue
delayfunc
=
self
.
delayfunc
timefunc
=
self
.
timefunc
pop
=
heapq
.
heappop
...
...
@@ -115,3 +118,17 @@ class scheduler:
delayfunc
(
0
)
# Let other threads run
else
:
heapq
.
heappush
(
event
)
@
property
def
queue
(
self
):
"""An ordered list of upcoming events.
Events are named tuples with fields for:
time, priority, action, arguments
"""
# Use heapq to sort the queue rather than using 'sorted(self._queue)'.
# With heapq, two events scheduled at the same time will show in
# the actual order they would be retrieved.
events
=
self
.
_queue
[:]
return
map
(
heapq
.
heappop
,
[
events
]
*
len
(
events
))
Misc/NEWS
View file @
44bd6c0a
...
...
@@ -364,6 +364,9 @@ Core and builtins
Library
-------
- #1861: Added an attribute to the sched module which returns an ordered
list of upcoming events (displayed as named tuples).
- #1837: The queue module now also supports a LIFO queue and a priority queue.
- Issue #1831: ctypes now raises a TypeError if conflicting positional
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment