【问题标题】:Python multiprocessing with starmap and issue with __main__带有星图的 Python 多处理和 __main__ 的问题
【发布时间】:2020-06-11 09:29:27
【问题描述】:

我试图用多个参数进行小型多处理

TaskType-1:

import multiprocessing  as mp
import pandas as pd
import os,sys
print("Libs Loaded..!!! ")
listoflists = [[1,2,3,4],[4,5,6,7],[7,8,9,10],[10,11,13,14]]
listoftuples = [tuple(i) for i in listoflists]
print("Length of Tuples : ",len(listoftuples))

def map_function(combo):
    a = combo[0]
    b = combo[1]
    c = combo[2]
    print((a + b + c))
    return (a + b + c)

def doit():
    try:
        print("IN main")
        p = mp.Pool(processes=2)
        # results= p.map(map_function, listoftuples)
        results = p.starmap(map_function,listoftuples)
        print(results)
        print("Done!!")
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)

# if '__name__' == '__main__': 
doit()     # running without entry point

TaskType-1 错误:

Libs Loaded..!!!
Length of Tuples :  4
IN main
Libs Loaded..!!!
Length of Tuples :  4
IN main
<class 'RuntimeError'> testformp.py 20
<class 'TypeError'> testformp.py 22
Libs Loaded..!!!
Length of Tuples :  4
IN main
<class 'RuntimeError'> testformp.py 20

不确定此运行时错误以及为什么它多次进入 doit() 函数。在此函数中定义了多重处理,但在这里它一次又一次地调用父函数..不确定我在这里缺少什么来理解?

TaskType-2:

if '__name__' == '__main__':
    doit()  # running from entry point

TaskType-2 的输出:

Libs Loaded..!!!
Length of Tuples :  4
    

它没有显示任何错误,也没有执行任何内部任务。为什么会这样?

【问题讨论】:

  • 您的意思是:if __name__ == '__main__':?否则doit() 将永远不会运行。
  • 另外TypeError 来自starmap() 处理参数参数的方式。您可能想要:def map_function(*combo): 因此 starmap
  • @quamrana ,即使我使用 (*combo) 获取参数仍然存在运行时错误?你能详细解释一下吗?如果我在 if name == 'main' : doit() 中运行我的脚本,那么 doit 函数不会启动多处理部分?为什么会这样?跨度>
  • 你用的是哪个版本的python?
  • @madhureddy,它的 Python 3.7.3

标签: python python-multiprocessing entry-point


【解决方案1】:

进行我建议的两个更改,我最终得到了这个:

import multiprocessing  as mp
import os,sys
#print("Libs Loaded..!!! ")
listoflists = [[1,2,3,4],[4,5,6,7],[7,8,9,10],[10,11,13,14]]
listoftuples = [tuple(i) for i in listoflists]
#print("Length of Tuples : ",len(listoftuples))

def map_function(*combo):
    a = combo[0]
    b = combo[1]
    c = combo[2]
    #print((a + b + c))
    return (a + b + c)

def doit():
    try:
        print("IN main")
        p = mp.Pool(processes=2)
        #results= p.map(map_function, listoftuples)
        results = p.starmap(map_function,listoftuples)
        print(results)
        print("Done!!")
    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)

if __name__ == '__main__':
    doit()     # running without entry point

输出是这样的:

IN main
[6, 15, 24, 34]
Done!!

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 2013-12-11
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多