简体   繁体   English

TypeError:对于多处理池,字符串索引必须为整数

[英]TypeError: string indices must be integers for Multiprocessing Pool

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 . 运行pool_testing()函数时,出现TypeError I tried changing pool = multiprocessing.Pool() to pool = multiprocessing.Pool(processes=n) , but same error. 我尝试将pool = multiprocessing.Pool()更改为pool = multiprocessing.Pool(processes=n) ,但是存在相同的错误。 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

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: pool.map自动迭代函数参数的每个项目地图 ,这样你就不需要做手工( for i in profile - ) profile :在功能描述中的相关行已经是你的项目感兴趣的

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: 因此,在您的情况下,您的testing功能如下所示:

def testing(profile):
    print "Hey " + str(profile["firstName"]) + "!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM