python 多处理 __getnewargs__()

python multiprocessing __getnewargs__()

import multiprocessing
import time

data = (
    ['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
    ['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)

def mp_worker(inputs, the_time):
    print(" Processs %s\tWaiting %s seconds" % (inputs, the_time))
    time.sleep(int(the_time))
    print(" Process %s\tDONE" % inputs)

def mp_handler():
    p = multiprocessing.Pool(2)
    p.map(mp_worker, data)

if __name__ == '__main__':
    mp_handler()

此代码在 3 python 下不起作用,本质上它适合我但发誓 TypeError: mp_worker () missing 1 required positional argument: 'the_time'

google了一下getnewargs(),这段代码怎么用不清楚,python一两个月就知道了

pool.map(f, a) 

替换

pool.starmap(f, a)

固定,比x

您需要使用 starmap 而不是 map 将多个参数从元组传递给函数。 From the Python documentation:

Like map() except that the elements of the iterable are expected to be iterables that are unpacked as arguments.

Hence an iterable of [(1,2), (3, 4)] results in [func(1,2), func(3,4)].