-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathqueue_tutorial.py
More file actions
70 lines (59 loc) · 1.77 KB
/
queue_tutorial.py
File metadata and controls
70 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from queue import Queue
# Queue is thread-safe.
# if the operation cannot successfully complete because the queue is either empty (cant get) or full ( cant put).
# The default behavior is to block or idly wait until the Queue object has data or room available to
# complete the operation.
# You can have it raise exceptions instead by passing the block=False parameter.
# Or you can have it wait a defined amount of time before raising an exception by passing a timeout parameter
def fifo_ex():
# FIFO
lineup = Queue(maxsize=3)
# lineup.get(block=False)
lineup.put(1)
lineup.put(2)
lineup.put(3)
# lineup.put(4, timeout=1)
print('lineup.full():', lineup.full())
print(lineup.get())
print(lineup.get())
print(lineup.get())
print('lineup.empty():', lineup.empty())
def lifo_ex():
# LIFO, also called stacks ()
# Applicable situation - concurrent
# why not use list
from queue import LifoQueue
stack = LifoQueue(maxsize=3)
stack.put(1)
stack.put(2)
stack.put(3)
# stack.put(4, block=False)
print(stack.get())
print(stack.get())
print(stack.get())
# stack.get(timeout=1)
def priority_queue_ex():
# data structure - heap
# Applicable situation - product recommendation
from queue import PriorityQueue
pq = PriorityQueue()
pq.put((5, 'c'))
pq.put((2, 'a'))
pq.put((1, 'b'))
pq.put((4, 'd'))
while not pq.empty():
print(pq.get())
def priority_queue_ex_2():
from queue import PriorityQueue
pq = PriorityQueue()
pq.put((-5, 'c'))
pq.put((-2, 'a'))
pq.put((-1, 'b'))
pq.put((-4, 'd'))
while not pq.empty():
print(pq.get())
if __name__ == "__main__":
fifo_ex()
# lifo_ex()
# priority_queue_ex()
# priority_queue_ex_2()