Distributed and decentralized pi calculation

add saving pi_val on error

+53 -42
+53 -42
src/client/client.py
··· 3 3 import time 4 4 import threading 5 5 import requests 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 - def calc(n, prec): 24 - getcontext().prec = prec 25 - t = Decimal(0) 26 - pi = Decimal(0) 27 - deno = Decimal(0) 28 - for k in range(n): 29 - t = ((-1)**k) * (factorial(6*k)) * (13591409 + 545140134*k) 30 - deno = factorial(3*k) * (factorial(k)**3) * (640320**(3*k)) 31 - pi += Decimal(t) / Decimal(deno) 32 - pi = pi * Decimal(12) / Decimal(640320**Decimal(1.5)) 33 - pi = 1/pi 34 - return pi 24 + try: 25 + def calc(n, prec): 26 + getcontext().prec = prec 27 + t = Decimal(0) 28 + pi = Decimal(0) 29 + deno = Decimal(0) 30 + for k in range(n): 31 + t = ((-1)**k) * (factorial(6*k)) * (13591409 + 545140134*k) 32 + deno = factorial(3*k) * (factorial(k)**3) * (640320**(3*k)) 33 + pi += Decimal(t) / Decimal(deno) 34 + pi = pi * Decimal(12) / Decimal(640320**Decimal(1.5)) 35 + pi = 1/pi 36 + return pi 35 37 36 - def upload(): 37 - while True: 38 - if str(pi_val).startswith("3.14") == True: 39 - print(">> checking and uploading latest calculation") 40 - response = requests.get(server + "/api/getpi") 41 - print(response.text) 42 - replen = len(response.text) 43 - pilen = len(str(pi_val)) 44 - if response.text == "No clients have connected yet. Become one of the first!": 45 - print("server pi doesn't exist! uploading") 46 - data = {'pi': str(pi_val)} 47 - print(data) 48 - response = requests.post(server + "/api/postpi", data=data) 38 + def upload(): 39 + while True: 40 + if str(pi_val).startswith("3.14") == True: 41 + print(">> checking and uploading latest calculation") 42 + response = requests.get(server + "/api/getpi") 49 43 print(response.text) 50 - else: 51 - if replen > pilen: 52 - print("server pi length is greater than local pi length! not uploading") 53 - elif pilen > replen: 54 - print("local pi length longer than server pi length! uploading") 44 + replen = len(response.text) 45 + pilen = len(str(pi_val)) 46 + if response.text == "No clients have connected yet. Become one of the first!": 47 + print("server pi doesn't exist! uploading") 55 48 data = {'pi': str(pi_val)} 49 + print(data) 56 50 response = requests.post(server + "/api/postpi", data=data) 57 51 print(response.text) 58 - else: 59 - print("pi_val not yet defined!") 60 - time.sleep(60) 52 + else: 53 + if replen > pilen: 54 + print("server pi length is greater than local pi length! not uploading") 55 + elif pilen > replen: 56 + print("local pi length longer than server pi length! uploading") 57 + data = {'pi': str(pi_val)} 58 + response = requests.post(server + "/api/postpi", data=data) 59 + print(response.text) 60 + else: 61 + print("pi_val not yet defined!") 62 + time.sleep(60) 61 63 62 - threading.Thread(target=upload, daemon=True).start() 64 + threading.Thread(target=upload, daemon=True).start() 63 65 64 - calculation = 1 65 - precision = 10 66 + calculation = 1 67 + precision = 10 66 68 67 - while True: 68 - pi_val = calc(calculation, precision) 69 - print(pi_val) 70 - calculation += 1 71 - precision += 5 72 - time.sleep(5) 69 + while True: 70 + pi_val = calc(calculation, precision) 71 + print(pi_val) 72 + calculation += 1 73 + precision += 5 74 + time.sleep(5) 75 + except: 76 + try: 77 + print("Saving your latest Pi to Pi.txt and exiting...") 78 + with open("pi.txt", "w") as f: 79 + f.write(str(pi_val)) 80 + sys.exit() 81 + except: 82 + print("Unable to save to pi.txt. This could be because pi_val wasn't defined yet. Exiting..") 83 + sys.exit()