MCPcopy Create free account
hub / github.com/geekcomputers/Python / pi

Function pi

communication/socket_conn.py:18–40  ·  view source on GitHub ↗
(n)

Source from the content-addressed store, hash-verified

16
17
18def pi(n):
19 childs = {}
20 unit = n / 10
21 for i in range(10): # 分10个子进程
22 mink = unit * i
23 maxk = mink + unit
24 rsock, wsock = socket.socketpair()
25 pid = os.fork()
26 if pid > 0:
27 childs[pid] = rsock
28 wsock.close()
29 else:
30 rsock.close()
31 s = slice(mink, maxk) # 子进程开始计算
32 wsock.send(str(s))
33 wsock.close()
34 sys.exit(0) # 子进程结束
35 sums = []
36 for pid, rsock in childs.items():
37 sums.append(float(rsock.recv(1024)))
38 rsock.close()
39 os.waitpid(pid, 0) # 等待子进程结束
40 return math.sqrt(sum(sums) * 8)
41
42
43print(pi(10000000))

Callers 1

socket_conn.pyFile · 0.70

Calls 3

sliceFunction · 0.70
closeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected