-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducer.py
36 lines (28 loc) · 1.02 KB
/
producer.py
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
import multiprocessing
from multiprocessing.managers import BaseManager
from multiprocessing import Process, current_process, Lock
import time
class QueueManager(BaseManager):
pass
QueueManager.register('get_queue')
QueueManager.register('get_max')
QueueManager.register('get_cnt')
num_producer = 2
pencil = Lock()
def producer(r_manger):
r_manger.connect()
serving_line = r_manger.get_queue()
size = r_manger.get_max().get("size")
i = r_manger.get_cnt()
while True:
with pencil:
print(
f'{current_process().name} produced #{i.value()} - remaining capacity: {size - serving_line.qsize()}')
serving_line.put(f"Bowl #{i.value()}")
i.increment()
# sleep can represent that the thread is doing some io task
time.sleep(0.2)
if __name__ == '__main__':
m = QueueManager(address=('localhost', 50000), authkey=b'abracadabra')
for i in range(num_producer):
Process(target=producer, args=(m,), name=f'Producer {i}').start()