使用starmap的Python多处理和__main__的问题。

问题描述 投票:0回答:1

我想做一个多参数的小型多处理。

任务类型-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()函数。在这个函数中定义了多处理,但在这里,它是一次又一次地调用父函数......不知道我在这里错过了什么理解?

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

TaskType-2的输出。

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

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

python python-multiprocessing entry-point
1个回答
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!!
© www.soinside.com 2019 - 2024. All rights reserved.