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
75404274
Commit
75404274
authored
Jan 07, 2012
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify the code using with-statements.
parent
2c94cdde
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
28 deletions
+14
-28
Lib/queue.py
Lib/queue.py
+14
-28
No files found.
Lib/queue.py
View file @
75404274
...
...
@@ -26,17 +26,21 @@ class Queue:
def
__init__
(
self
,
maxsize
=
0
):
self
.
maxsize
=
maxsize
self
.
_init
(
maxsize
)
# mutex must be held whenever the queue is mutating. All methods
# that acquire mutex must release it before returning. mutex
# is shared between the three conditions, so acquiring and
# releasing the conditions also acquires and releases mutex.
self
.
mutex
=
_threading
.
Lock
()
# Notify not_empty whenever an item is added to the queue; a
# thread waiting to get is notified then.
self
.
not_empty
=
_threading
.
Condition
(
self
.
mutex
)
# Notify not_full whenever an item is removed from the queue;
# a thread waiting to put is notified then.
self
.
not_full
=
_threading
.
Condition
(
self
.
mutex
)
# Notify all_tasks_done whenever the number of unfinished tasks
# drops to zero; thread waiting to join() is notified to resume
self
.
all_tasks_done
=
_threading
.
Condition
(
self
.
mutex
)
...
...
@@ -56,16 +60,13 @@ class Queue:
Raises a ValueError if called more times than there were items
placed in the queue.
"""
self
.
all_tasks_done
.
acquire
()
try
:
with
self
.
all_tasks_done
:
unfinished
=
self
.
unfinished_tasks
-
1
if
unfinished
<=
0
:
if
unfinished
<
0
:
raise
ValueError
(
'task_done() called too many times'
)
self
.
all_tasks_done
.
notify_all
()
self
.
unfinished_tasks
=
unfinished
finally
:
self
.
all_tasks_done
.
release
()
def
join
(
self
):
"""Blocks until all items in the Queue have been gotten and processed.
...
...
@@ -76,19 +77,14 @@ class Queue:
When the count of unfinished tasks drops to zero, join() unblocks.
"""
self
.
all_tasks_done
.
acquire
()
try
:
with
self
.
all_tasks_done
:
while
self
.
unfinished_tasks
:
self
.
all_tasks_done
.
wait
()
finally
:
self
.
all_tasks_done
.
release
()
def
qsize
(
self
):
"""Return the approximate size of the queue (not reliable!)."""
self
.
mutex
.
acquire
()
n
=
self
.
_qsize
()
self
.
mutex
.
release
()
return
n
with
self
.
mutex
:
return
self
.
_qsize
()
def
empty
(
self
):
"""Return True if the queue is empty, False otherwise (not reliable!).
...
...
@@ -102,10 +98,8 @@ class Queue:
completed, the preferred technique is to use the join() method.
"""
self
.
mutex
.
acquire
()
n
=
not
self
.
_qsize
()
self
.
mutex
.
release
()
return
n
with
self
.
mutex
:
return
not
self
.
_qsize
()
def
full
(
self
):
"""Return True if the queue is full, False otherwise (not reliable!).
...
...
@@ -116,10 +110,8 @@ class Queue:
qsize() can be used.
"""
self
.
mutex
.
acquire
()
n
=
0
<
self
.
maxsize
<=
self
.
_qsize
()
self
.
mutex
.
release
()
return
n
with
self
.
mutex
:
return
0
<
self
.
maxsize
<=
self
.
_qsize
()
def
put
(
self
,
item
,
block
=
True
,
timeout
=
None
):
"""Put an item into the queue.
...
...
@@ -132,8 +124,7 @@ class Queue:
is immediately available, else raise the Full exception ('timeout'
is ignored in that case).
"""
self
.
not_full
.
acquire
()
try
:
with
self
.
not_full
:
if
self
.
maxsize
>
0
:
if
not
block
:
if
self
.
_qsize
()
>=
self
.
maxsize
:
...
...
@@ -153,8 +144,6 @@ class Queue:
self
.
_put
(
item
)
self
.
unfinished_tasks
+=
1
self
.
not_empty
.
notify
()
finally
:
self
.
not_full
.
release
()
def
put_nowait
(
self
,
item
):
"""Put an item into the queue without blocking.
...
...
@@ -175,8 +164,7 @@ class Queue:
available, else raise the Empty exception ('timeout' is ignored
in that case).
"""
self
.
not_empty
.
acquire
()
try
:
with
self
.
not_empty
:
if
not
block
:
if
not
self
.
_qsize
():
raise
Empty
...
...
@@ -195,8 +183,6 @@ class Queue:
item
=
self
.
_get
()
self
.
not_full
.
notify
()
return
item
finally
:
self
.
not_empty
.
release
()
def
get_nowait
(
self
):
"""Remove and return an item from the queue without blocking.
...
...
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