Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
9e1bc982
Kaydet (Commit)
9e1bc982
authored
Ock 16, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add queues will alternative fetch orders (priority based and stack based).
üst
171f3916
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
14 deletions
+57
-14
Queue.py
Lib/Queue.py
+38
-2
test_queue.py
Lib/test/test_queue.py
+19
-12
No files found.
Lib/Queue.py
Dosyayı görüntüle @
9e1bc982
...
...
@@ -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()."
...
...
@@ -196,7 +197,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
...
...
@@ -206,3 +207,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
()
Lib/test/test_queue.py
Dosyayı görüntüle @
9e1bc982
...
...
@@ -181,8 +181,13 @@ def SimpleQueueTest(q):
raise
RuntimeError
,
"Call this function with an empty queue"
# I guess we better check things actually queue correctly a little :)
q
.
put
(
111
)
q
.
put
(
333
)
q
.
put
(
222
)
verify
(
q
.
get
()
==
111
and
q
.
get
()
==
222
,
target_order
=
dict
(
Queue
=
[
111
,
333
,
222
],
LifoQueue
=
[
222
,
333
,
111
],
PriorityQueue
=
[
111
,
222
,
333
])
actual_order
=
[
q
.
get
(),
q
.
get
(),
q
.
get
()]
verify
(
actual_order
==
target_order
[
q
.
__class__
.
__name__
],
"Didn't seem to queue the correct data!"
)
for
i
in
range
(
QUEUE_SIZE
-
1
):
q
.
put
(
i
)
...
...
@@ -260,18 +265,20 @@ def QueueTaskDoneTest(q):
raise
TestFailed
(
"Did not detect task count going negative"
)
def
test
():
q
=
Queue
.
Queue
()
QueueTaskDoneTest
(
q
)
QueueJoinTest
(
q
)
QueueJoinTest
(
q
)
QueueTaskDoneTest
(
q
)
for
Q
in
Queue
.
Queue
,
Queue
.
LifoQueue
,
Queue
.
PriorityQueue
:
q
=
Q
()
QueueTaskDoneTest
(
q
)
QueueJoinTest
(
q
)
QueueJoinTest
(
q
)
QueueTaskDoneTest
(
q
)
q
=
Q
(
QUEUE_SIZE
)
# Do it a couple of times on the same queue
SimpleQueueTest
(
q
)
SimpleQueueTest
(
q
)
if
verbose
:
print
"Simple Queue tests seemed to work for"
,
Q
.
__name__
q
=
Queue
.
Queue
(
QUEUE_SIZE
)
# Do it a couple of times on the same queue
SimpleQueueTest
(
q
)
SimpleQueueTest
(
q
)
if
verbose
:
print
"Simple Queue tests seemed to work"
q
=
FailingQueue
(
QUEUE_SIZE
)
FailingQueueTest
(
q
)
FailingQueueTest
(
q
)
...
...
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