+ 1

How to compute Pi using multiprocessing module?

I'd like to make this function use multiprocessing for the first 3 steps in the while loop, which are independent of each other. I can't see how to do that without creating functions. I don't know how to do it with functions either since I don't think local variables can be modified externally. The code is from the Python documentation for the decimal module. def pi(): """Compute Pi to the current precision.""" getcontext().prec += 2 # extra digits for intermediate steps three = Decimal(3) # substitute "three=3.0" for regular floats lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 while s != lasts: lasts = s n, na = n+na, na+8 d, da = d+da, da+32 t = (t * n) / d s += t getcontext().prec -= 2 return +s # unary plus applies the new precision 

22nd Apr 2021, 11:02 PM
Sheikh
Sheikh - avatar
2 Antworten
+ 1
Im not sure about the beggining of your problem, but if local variables are the problem, Id resort to making this code in OOP so references are irrelevent inside the class, or just pass the variables as parameters to the function. It wont have to be local variables if you set the function up to deal with every variable it needs to externally modify. OR you can set the variables inside the function as global, not recommended but it would work
23rd Apr 2021, 1:43 AM
Slick
Slick - avatar
0
Slick I actually managed to make this after posting the question: https://code.sololearn.com/cZdQFB4chKPB/?ref=app However, it just made the code run slower, at least here on SoloLearn. Any suggestions to optimize it? I have yet to test it on my laptop.
23rd Apr 2021, 7:17 AM
Sheikh
Sheikh - avatar