Reputation: 9
I'm trying to figure out how to use multiprocessing, but having an issue with the following code. When running the pool_testing()
function I get a TypeError
. I tried changing pool = multiprocessing.Pool()
to pool = multiprocessing.Pool(processes=n)
, but same error. Can anybody please help?
import multiprocessing
profile = [{u'firstName': u'Karen', u'age': 20},
{u'firstName': u'Jon', u'age': 25}]
def testing(profile):
for i in profile:
print ("Hey " + str(i["firstName"]) + "!")
def pool_testing():
pool = multiprocessing.Pool()
pool.map(testing, profile)
pool_testing()
Traceback:
File "/System/.../multiprocessing/pool.py", line 567, in get
raise self._value
TypeError: string indices must be integers
Upvotes: 0
Views: 887
Reputation: 8999
pool.map
automatically maps each item of the iterable argument to the function, so you don't need to do it manually (for i in profile
) - profile
is already the item your interested in. The relevant line in the function description:
This method chops the iterable into a number of chunks which it submits to the process pool as separate tasks.
So, in your case your testing
function would look like:
def testing(profile):
print "Hey " + str(profile["firstName"]) + "!"
Upvotes: 1