Multiprocessing with multiple arguments to function in Python 2.7

后端 未结 3 1399
Happy的楠姐
Happy的楠姐 2021-01-06 04:08

I\'m trying to implement multiprocessing to speed up a replication loop, but cannot get it to work in Python27. This is a very simplified version of my program, based on the

3条回答
  •  情深已故
    2021-01-06 05:02

    The problem is solved by adding a main() function as:

    import itertools
    from multiprocessing import Pool
    
    def func(g, h, i):
        return g + h + i
    
    def helper(args):
        args2 = args[0] + (args[1],)
        return func(*args2)
    
    def main():
        pool = Pool(processes=4)
        result = pool.map(helper,itertools.izip(itertools.repeat((2, 3)), range(10)))
        print result
    
    if __name__ == '__main__':
        main()
    

    Based on the answer from @ErikAllik I'm thinking that this might be a Windows-specific problem.

    edit: Here is a clear and informative tutorial on multiprocessing in python.

提交回复
热议问题