Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I'm trying to do a multiprocessed version of Monte Carlo Pi calculation.
However, I always get an error that says
TypeError: map() missing 1 required positional argument: 'iterable'
I read that the error can be fixed by using starmap but it doesn't work either for some reason.
%%timeit
import random
import math
import itertools
import multiprocessing
from multiprocessing import Pool, current_process
from timeit import default_timer as timer
import functools
def monteCarlo(total):
inside = 0
for i in range(0, total):
x2 = random.random()**2
y2 = random.random()**2
if math.sqrt(x2 + y2) < 1.0:
inside += 1
pi = (float(inside) / total) * 4
return pi
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
values = [100]
data = sum(pool.map(functools.partial(monteCarlo, values)))
print(data)
–
–
–
Don't need to call partial to get map value partial rather use just pool.map
. The partial()
is used for partial function application which “freezes” some portion of a function’s arguments and/or keywords resulting in a new object with a simplified signature. But you don't have need something freeze
, if further need to use partial then you need to pass two argument to get the callable beahave. Get more about functools
import random
import math
import itertools
import multiprocessing
from multiprocessing import Pool, current_process
from timeit import default_timer as timer
import functools
def monteCarlo(total):
inside = 0
for i in range(0, total):
x2 = random.random()**2
y2 = random.random()**2
if math.sqrt(x2 + y2) < 1.0:
inside += 1
pi = (float(inside) / total) * 4
return pi
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
values = [100]
data = sum(pool.map(monteCarlo, values))
print(data)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.