簡體   English   中英

TypeError:對於多處理池,字符串索引必須為整數

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

我試圖弄清楚如何使用多重處理,但是以下代碼有問題。 運行pool_testing()函數時,出現TypeError 我嘗試將pool = multiprocessing.Pool()更改為pool = multiprocessing.Pool(processes=n) ,但是存在相同的錯誤。 有人可以幫忙嗎?

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()

追溯:

File "/System/.../multiprocessing/pool.py", line 567, in get
    raise self._value
TypeError: string indices must be integers

pool.map自動迭代函數參數的每個項目地圖 ,這樣你就不需要做手工( for i in profile - ) profile :在功能描述中的相關行已經是你的項目感興趣的

此方法將可迭代項分為多個塊,將其作為單獨的任務提交給流程池。

因此,在您的情況下,您的testing功能如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM