-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathexer06b01_blocking_queue.py
More file actions
67 lines (43 loc) · 1.37 KB
/
Copy pathexer06b01_blocking_queue.py
File metadata and controls
67 lines (43 loc) · 1.37 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
'''
BLOCKING QUEUE IMPLEMENTATION
Version B01: General blocking queues
Underlying mechanism: Semaphores
'''
import time
import threading
class BlockingQueue:
def __init__(self, capacity: int):
if capacity <= 0:
raise ValueError('capacity must be a positive integer')
# self.__capacity = capacity
self.__sem_remain = threading.Semaphore(capacity)
self.__sem_fill = threading.Semaphore(0)
self.__lk = threading.Lock()
self.__q = [] # queue
def put(self, value):
self.__sem_remain.acquire()
with self.__lk:
self.__q.append(value)
self.__sem_fill.release()
def take(self):
self.__sem_fill.acquire()
with self.__lk:
result = self.__q.pop(0)
self.__sem_remain.release()
return result
def producer(q: BlockingQueue):
arr = [ 'nice', 'to', 'meet', 'you' ]
for data in arr:
print(f'Producer: {data}')
q.put(data)
print(f'Producer: {data} \t\t\t[done]')
def consumer(q: BlockingQueue):
time.sleep(5)
for i in range(4):
data = q.take()
print(f'\tConsumer: {data}')
if i == 0:
time.sleep(5)
blkqueue = BlockingQueue(2) # capacity = 2
threading.Thread(target=producer, args=(blkqueue,)).start()
threading.Thread(target=consumer, args=(blkqueue,)).start()