-
Notifications
You must be signed in to change notification settings - Fork 0
/
les_39_multprocess.py
38 lines (33 loc) · 1.13 KB
/
les_39_multprocess.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
37
38
import multiprocessing
class WarehouseManager:
def __init__(self):
manager = multiprocessing.Manager()
self.data = manager.dict()
def process_request(self, request):
action, product, quantity = request
if action == "receipt":
if product in self.data:
self.data[product] += quantity
else:
self.data[product] = quantity
elif action == "shipment":
if product in self.data and self.data[product] >= quantity:
self.data[product] -= quantity
def run(self, requests):
processes = []
for request in requests:
process = multiprocessing.Process(target=self.process_request, args=(request,))
processes.append(process)
process.start()
for process in processes:
process.join()
if __name__ == "__main__":
_requests = [
("receipt", "product1", 10),
("shipment", "product1", 5),
("receipt", "product2", 20),
("shipment", "product2", 15),
]
manager = WarehouseManager()
manager.run(_requests)
print(manager.data)