tangled
alpha
login
or
join now
wish13yt.github.io
/
pistributed
0
fork
atom
Distributed and decentralized pi calculation
0
fork
atom
overview
issues
pulls
pipelines
add saving pi_val on error
wish13yt.github.io
5 months ago
6fdea786
b2eedee7
+53
-42
1 changed file
expand all
collapse all
unified
split
src
client
client.py
+53
-42
src/client/client.py
···
3
3
import time
4
4
import threading
5
5
import requests
6
6
+
import sys
6
7
7
8
print("Pistributed, an app to calculate Pi with distributed power.")
8
9
print("Do not run on systems owned by another entity, entities, person, or people, without explicit permission from them.")
···
20
21
print("-------------------")
21
22
global pi_val
22
23
pi_val = None
23
23
-
def calc(n, prec):
24
24
-
getcontext().prec = prec
25
25
-
t = Decimal(0)
26
26
-
pi = Decimal(0)
27
27
-
deno = Decimal(0)
28
28
-
for k in range(n):
29
29
-
t = ((-1)**k) * (factorial(6*k)) * (13591409 + 545140134*k)
30
30
-
deno = factorial(3*k) * (factorial(k)**3) * (640320**(3*k))
31
31
-
pi += Decimal(t) / Decimal(deno)
32
32
-
pi = pi * Decimal(12) / Decimal(640320**Decimal(1.5))
33
33
-
pi = 1/pi
34
34
-
return pi
24
24
+
try:
25
25
+
def calc(n, prec):
26
26
+
getcontext().prec = prec
27
27
+
t = Decimal(0)
28
28
+
pi = Decimal(0)
29
29
+
deno = Decimal(0)
30
30
+
for k in range(n):
31
31
+
t = ((-1)**k) * (factorial(6*k)) * (13591409 + 545140134*k)
32
32
+
deno = factorial(3*k) * (factorial(k)**3) * (640320**(3*k))
33
33
+
pi += Decimal(t) / Decimal(deno)
34
34
+
pi = pi * Decimal(12) / Decimal(640320**Decimal(1.5))
35
35
+
pi = 1/pi
36
36
+
return pi
35
37
36
36
-
def upload():
37
37
-
while True:
38
38
-
if str(pi_val).startswith("3.14") == True:
39
39
-
print(">> checking and uploading latest calculation")
40
40
-
response = requests.get(server + "/api/getpi")
41
41
-
print(response.text)
42
42
-
replen = len(response.text)
43
43
-
pilen = len(str(pi_val))
44
44
-
if response.text == "No clients have connected yet. Become one of the first!":
45
45
-
print("server pi doesn't exist! uploading")
46
46
-
data = {'pi': str(pi_val)}
47
47
-
print(data)
48
48
-
response = requests.post(server + "/api/postpi", data=data)
38
38
+
def upload():
39
39
+
while True:
40
40
+
if str(pi_val).startswith("3.14") == True:
41
41
+
print(">> checking and uploading latest calculation")
42
42
+
response = requests.get(server + "/api/getpi")
49
43
print(response.text)
50
50
-
else:
51
51
-
if replen > pilen:
52
52
-
print("server pi length is greater than local pi length! not uploading")
53
53
-
elif pilen > replen:
54
54
-
print("local pi length longer than server pi length! uploading")
44
44
+
replen = len(response.text)
45
45
+
pilen = len(str(pi_val))
46
46
+
if response.text == "No clients have connected yet. Become one of the first!":
47
47
+
print("server pi doesn't exist! uploading")
55
48
data = {'pi': str(pi_val)}
49
49
+
print(data)
56
50
response = requests.post(server + "/api/postpi", data=data)
57
51
print(response.text)
58
58
-
else:
59
59
-
print("pi_val not yet defined!")
60
60
-
time.sleep(60)
52
52
+
else:
53
53
+
if replen > pilen:
54
54
+
print("server pi length is greater than local pi length! not uploading")
55
55
+
elif pilen > replen:
56
56
+
print("local pi length longer than server pi length! uploading")
57
57
+
data = {'pi': str(pi_val)}
58
58
+
response = requests.post(server + "/api/postpi", data=data)
59
59
+
print(response.text)
60
60
+
else:
61
61
+
print("pi_val not yet defined!")
62
62
+
time.sleep(60)
61
63
62
62
-
threading.Thread(target=upload, daemon=True).start()
64
64
+
threading.Thread(target=upload, daemon=True).start()
63
65
64
64
-
calculation = 1
65
65
-
precision = 10
66
66
+
calculation = 1
67
67
+
precision = 10
66
68
67
67
-
while True:
68
68
-
pi_val = calc(calculation, precision)
69
69
-
print(pi_val)
70
70
-
calculation += 1
71
71
-
precision += 5
72
72
-
time.sleep(5)
69
69
+
while True:
70
70
+
pi_val = calc(calculation, precision)
71
71
+
print(pi_val)
72
72
+
calculation += 1
73
73
+
precision += 5
74
74
+
time.sleep(5)
75
75
+
except:
76
76
+
try:
77
77
+
print("Saving your latest Pi to Pi.txt and exiting...")
78
78
+
with open("pi.txt", "w") as f:
79
79
+
f.write(str(pi_val))
80
80
+
sys.exit()
81
81
+
except:
82
82
+
print("Unable to save to pi.txt. This could be because pi_val wasn't defined yet. Exiting..")
83
83
+
sys.exit()