Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

python - Add result from multiprocessing into array

I have a problem in when using results from multiprocessing in Python:

worker = math.ceil(lenRes1/maxRecord)
print(worker)
p=multiprocessing.Pool(worker)
while i < len(res):
    if(maxRecord > len(res)):
        t1arg = res[i:maxRecord]
    else:
        t1arg = res[i:maxRecord]
    #print(str(worker) + str(i) + str(lenRes1) + str(maxRecord))
    result = p.starmap_async(insertSingleObl.joinRes,[(t1arg,str(th),resJoinCorCustomer)])
    resFin.append(result.get)
    #p.start()
    i += RecordProcess
    maxRecord += RecordProcess
    th += 1
p.close()
p.join()

When I want to use it in a function, the list becomes:

[[array([[None, '010200189217', '010200189217', ..., None, None, '36'],
       [None, '010500191817', '010500191817', ..., None, None, '120'],
       [None, '030400254616', '030400254616', ..., None, None, '0'],
       ...,
       [None, '040818000867', '040818000867', ..., None, None, '432'],
       [None, '040318001291', '040318001291', ..., None, None, '0'],
       [None, '010218000652', '010218000652', ..., None, None, '0']],
      dtype=object)], [array([[None, '040117001434', '040117001434', ..., None, None, '144'],
       [None, '020200076017', '020200076017', ..., None, None, '120'],
       [None, '080118000512', '080118000512', ..., None, None, '84'],
       ...,
       [None, '060618001091', '060618001091', ..., None, None, '96'],
       [None, '010917004476', '010917004476', ..., None, None, '24'],
       [None, '020818004086', '020818004086', ..., None, None, '120']],
      dtype=object)], [array([[None, '011518002261', '011518002261', ..., None, None, '24'],
       [None, '040817004980', '040817004980', ..., None, None, '240'],
       [None, '010218002187', '010218002187', ..., None, None, '36'],
       ...,
       [None, '040218001345', '040218001345', ..., None, None, '360'],
       [None, '010718009910', '010718009910', ..., None, None, '120'],
       [None, '010200219216', '010200219216', ..., None, None, '60']],
      dtype=object)], [array([[None, '010218005523', '010218005523', ..., None, None, '432'],
       [None, '020317001165', '020317001165', ..., None, None, '276'],
       [None, '040318007804', '040318007804', ..., None, None, '516'],
       ...,
       [None, '010719001124', '010719001124', ..., None, None, '120'],
       [None, '040319000156', '040319000156', ..., None, None, '300'],
       [None, '010200219216', '010200219216', ..., None, None, '60']],
      dtype=object)], [array([[None, '010418000416', '010418000416', ..., None, None, '120'],
       [None, '030119002393', '030119002393', ..., None, None, '240'],
       [None, '011119000058', '011119000058', ..., None, None, '96'],
       ...,
       [None, '030318001163', '030318001163', ..., None, None, '120'],
       [None, '010719005551', '010719005551', ..., None, None, '24'],
       [None, '021019002274', '021019002274', ..., None, None, '108']],
      dtype=object)], [array([[None, '030119004819', '030119004819', ..., None, None, '588'],
       [None, '020718000515', '020718000515', ..., None, None, '36'],
       [None, '030417000028', '030417000028', ..., None, None, '60'],
       ...,
       [None, '021219000096', '021219000096', ..., None, None, '72'],
       [None, '020119000911', '020119000911', ..., None, None, None],
       [None, '020118006997', '020118006997', ..., None, None, None]],
      dtype=object)], [array([[None, '080118001797', '080118001797', ..., None, None, '180'],
       [None, '040719000481', '040719000481', ..., None, None, '240'],
       [None, '030319006378', '030319006378', ..., None, None, '432'],
       ...,
       [None, '010319003340', '010319003340', ..., None, None, '240'],
       [None, '020319003490', '020319003490', ..., None, None, '420'],
       [None, '040517002432', '040517002432', ..., None, None, '384']],
      dtype=object)], [array([[None, '010218004496', '010218004496', ..., None, None, None],
       [None, '040420000379', '040420000379', ..., None, None, '24'],
       [None, '020819002225', '020819002225', ..., None, None, '36'],
       ...,
       [None, '011519001965', '011519001965', ..., None, None, None],
       [None, '011519001965', '011519001965', ..., None, None, None],
       [None, '011519001965', '011519001965', ..., None, None, None]],
      dtype=object)], [array([[None, '011519001965', '011519001965', ..., None, None, None],
       [None, '011519001965', '011519001965', ..., None, None, None],
       [None, '011519001965', '011519001965', ..., None, None, None],
       ...,
       [None, '040518004670', '040518004670', ..., None, None, '120'],
       [None, '030400273116', '030400273116', ..., None, None, None],
       [None, '040220000661', '040220000661', ..., None, None, '240']],
      dtype=object)], [array([[None, '021118000633', '021118000633', ..., None, None, '552'],
       [None, '041218003041', '041218003041', ..., None, None, '84'],
       [None, '020419007289', '020419007289', ..., None, None, '180'],
       ...,
       [None, '030119001498', '030119001498', ..., None, None, '180'],
       [None, '040319005982', '040319005982', ..., None, None, '240'],
       [None, '060218004047', '060218004047', ..., None, None, '480']],
      dtype=object)]]

IndexError: list index out of range

It's because I need to run line-by-line:

for line in resFin:
  xxxx

Can someone help me with this problem? The problem is I need to append the result from each thread into one list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...