-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_pipe_queue.py
More file actions
73 lines (55 loc) · 1.85 KB
/
9_pipe_queue.py
File metadata and controls
73 lines (55 loc) · 1.85 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
71
72
73
from multiprocessing import Process, current_process, Queue, Pipe
import os
from concurrent.futures import ProcessPoolExecutor, as_completed
import time
def count_hundred_millions_with_queue(process_id:int, q: Queue):
pid = os.getpid()
process_name = current_process().name
temp = 0
for i in range(1, 100000001):
""" If you want to see the work-flow, uncomment it """
print(f"Process: {process_id}, Process Name: {process_name}, PID: {pid}, Added = {i}\n", end="")
temp += 1
q.put(temp)
print(f"Process: {process_id} Done")
def count_hundred_millions_with_pipe(process_id:int, p: Pipe):
pid = os.getpid()
process_name = current_process().name
temp = 0
for i in range(1, 100000001):
""" If you want to see the work-flow, uncomment it """
# print(f"Process: {process_id}, Process Name: {process_name}, PID: {pid}, Added = {i}\n", end="")
temp += 1
p.send(temp)
p.close()
print(f"Child Process Done")
def main():
# Queue
start_time = time.time()
queue = Queue()
processes = []
for i in range(1, 6):
p = Process(name=(str(i)), target=count_hundred_millions_with_queue, args=(i, queue))
processes.append(p)
p.start()
for process in processes:
process.join()
queue.put("exit")
total = 0
while True:
tmp = queue.get()
if tmp == "exit":
break
total += tmp
print(f"Total = {total}, {time.time() - start_time} Seconds")
# Pipe
# start_time = time.time()
# parent_conn, child_conn = Pipe()
#
# p = Process(name=("Child"), target=count_hundred_millions_with_pipe, args=(1, child_conn))
# p.start()
# p.join()
#
# print(f"Result = {parent_conn.recv()}, Parent Process Done, {time.time() - start_time} Seconds")
if __name__ == "__main__":
main()