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
35641461
Commit
35641461
authored
Jan 17, 2008
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sync-up Queue.py with Py2.6
parent
bae82b8a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
9 deletions
+66
-9
Doc/library/queue.rst
Doc/library/queue.rst
+28
-7
Lib/Queue.py
Lib/Queue.py
+38
-2
No files found.
Doc/library/queue.rst
View file @
35641461
...
...
@@ -6,23 +6,46 @@
:synopsis: A synchronized queue class.
The :mod:`Queue` module implements
a multi-producer, multi-consumer FIFO queue
.
The :mod:`Queue` module implements
multi-producer, multi-consumer queues
.
It is especially useful in threaded programming when information must be
exchanged safely between multiple threads. The :class:`Queue` class in this
module implements all the required locking semantics. It depends on the
availability of thread support in Python; see the :mod:`threading`
module.
The :mod:`Queue` module defines the following class and exception:
Implements three types of queue whose only difference is the order that
the entries are retrieved. In a FIFO queue, the first tasks added are
the first retrieved. In a LIFO queue, the most recently added entry is
the first retrieved (operating like a stack). With a priority queue,
the entries are kept sorted (using the :mod:`heapq` module) and the
lowest valued entry is retrieved first.
The :mod:`Queue` module defines the following classes and exceptions:
.. class:: Queue(maxsize)
Constructor for
the class
. *maxsize* is an integer that sets the upperbound
Constructor for
a FIFO queue
. *maxsize* is an integer that sets the upperbound
limit on the number of items that can be placed in the queue. Insertion will
block once this size has been reached, until queue items are consumed. If
*maxsize* is less than or equal to zero, the queue size is infinite.
.. class:: LifoQueue(maxsize)
Constructor for a LIFO queue. *maxsize* is an integer that sets the upperbound
limit on the number of items that can be placed in the queue. Insertion will
block once this size has been reached, until queue items are consumed. If
*maxsize* is less than or equal to zero, the queue size is infinite.
.. class:: PriorityQueue(maxsize)
Constructor for a priority queue. *maxsize* is an integer that sets the upperbound
limit on the number of items that can be placed in the queue. Insertion will
block once this size has been reached, until queue items are consumed. If
*maxsize* is less than or equal to zero, the queue size is infinite.
The lowest valued entries are retrieved first (the lowest valued entry is the
one returned by ``sorted(list(entries))[0]``). A typical pattern for entries
is a tuple in the form: ``(priority_number, data)``.
.. exception:: Empty
...
...
@@ -41,10 +64,8 @@ The :mod:`Queue` module defines the following class and exception:
Queue Objects
-------------
Class :class:`Queue` implements queue objects and has the methods described
below. This class can be derived from in order to implement other queue
organizations (e.g. stack) but the inheritable interface is not described here.
See the source code for details. The public methods are:
Queue objects (:class:``Queue``, :class:``LifoQueue``, or :class:``PriorityQueue``
provide the public methods described below.
.. method:: Queue.qsize()
...
...
Lib/Queue.py
View file @
35641461
...
...
@@ -2,8 +2,9 @@
from
time
import
time
as
_time
from
collections
import
deque
import
heapq
__all__
=
[
'Empty'
,
'Full'
,
'Queue'
]
__all__
=
[
'Empty'
,
'Full'
,
'Queue'
,
'PriorityQueue'
,
'LifoQueue'
]
class
Empty
(
Exception
):
"Exception raised by Queue.get(block=0)/get_nowait()."
...
...
@@ -182,7 +183,7 @@ class Queue:
def
_init
(
self
,
maxsize
):
self
.
queue
=
deque
()
def
_qsize
(
self
):
def
_qsize
(
self
,
len
=
len
):
return
len
(
self
.
queue
)
# Put a new item in the queue
...
...
@@ -192,3 +193,38 @@ class Queue:
# Get an item from the queue
def
_get
(
self
):
return
self
.
queue
.
popleft
()
class
PriorityQueue
(
Queue
):
'''Variant of Queue that retrieves open entries in priority order (lowest first).
Entries are typically tuples of the form: (priority number, data).
'''
def
_init
(
self
,
maxsize
):
self
.
queue
=
[]
def
_qsize
(
self
,
len
=
len
):
return
len
(
self
.
queue
)
def
_put
(
self
,
item
,
heappush
=
heapq
.
heappush
):
heappush
(
self
.
queue
,
item
)
def
_get
(
self
,
heappop
=
heapq
.
heappop
):
return
heappop
(
self
.
queue
)
class
LifoQueue
(
Queue
):
'''Variant of Queue that retrieves most recently added entries first.'''
def
_init
(
self
,
maxsize
):
self
.
queue
=
[]
def
_qsize
(
self
,
len
=
len
):
return
len
(
self
.
queue
)
def
_put
(
self
,
item
):
self
.
queue
.
append
(
item
)
def
_get
(
self
):
return
self
.
queue
.
pop
()
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