2016-09-30 14 views
2

を私はPythonのmultiprocessing.Poolモジュールとマルチプロセッシングを行うが、TypeError: list indices must be integers, not strエラーました:ここマルチプロセッシングpool.map() "はTypeError:リストインデックスは整数でなければなりません、ないをstr" しまった

が私のコードです:

def getData(qid): 
    r = requests.get("http://api.xxx.com/api?qid=" + qid) 
    if r.status == 200: 
     DBC.save(json.loads(r.text)) 

def getAnotherData(qid): 
    r = requests.get("http://api.xxxx.com/anotherapi?qid=" + qid) 
    if r.status == 200: 
     DBC.save(json.loads(r.text)) 

def getAllData(qid): 
    print qid 
    getData(str(qid)) 
    getAnotherData(str(qid)) 


if __name__ == "__main__": 
    pool = Pool(processes=200) 
    pool.map(getAllData, range(10000, 700000)) 

は、いくつかの時間(ない瞬時に)のためのコードを実行した後、例外が間違っている可能性が何

pool.map(getAllData, range(10000, 700000)) 
    File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map 
    return self.map_async(func, iterable, chunksize).get() 
    File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get 
    raise self._value 
TypeError: list indices must be integers, not str 

をスローされますか?それはPoolモジュールの不具合ですか?

答えて

3

ワーカータスクで例外が発生すると、Poolがそれをキャッチして親プロセスに送り返し、例外を再発行しますが、これは元のトレースバックを保持しません(親でリレイプロセスは非常に有用ではありません)。推測すると、DBC.saveには、JSONからロードされた値がintになると予想され、実際にはstrです。

あなたは本当のトレースバックを表示したい場合は、トップレベルにimport traceback、とのおワーカー機能のトップレベルを変更しますので、あなただけではなく、労働者に本当のトレースバックを見ることができます

def getAllData(qid): 
    try: 
     print qid 
     getData(str(qid)) 
     getAnotherData(str(qid)) 
    except: 
     traceback.print_exc() 
     raise 

親の中でほとんど無益な逆戻り。

+0

ありがとう、男、本当に私に手がかりを与える! – armnotstrong

関連する問題