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

Function pi

communication/file.py:16–36  ·  view source on GitHub ↗
(n)

Source from the content-addressed store, hash-verified

14
15
16def pi(n):
17 pids = []
18 unit = n / 10
19 for i in range(10): # 分10个子进程
20 mink = unit * i
21 maxk = mink + unit
22 pid = os.fork()
23 if pid > 0:
24 pids.append(pid)
25 else:
26 s = slice(mink, maxk) # 子进程开始计算
27 with open("%d" % os.getpid(), "w") as f:
28 f.write(str(s))
29 sys.exit(0) # 子进程结束
30 sums = []
31 for pid in pids:
32 os.waitpid(pid, 0) # 等待子进程结束
33 with open("%d" % pid, "r") as f:
34 sums.append(float(f.read()))
35 os.remove("%d" % pid) # 删除通信的文件
36 return math.sqrt(sum(sums) * 8)
37
38
39if __name__ == "__main__":

Callers 1

file.pyFile · 0.70

Calls 3

sliceFunction · 0.70
appendMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected