How to use multiprocessing pool.map with multiple arguments?

前端 未结 20 3444
-上瘾入骨i
-上瘾入骨i 2020-11-21 11:24

In the Python multiprocessing library, is there a variant of pool.map which supports multiple arguments?

text = "test"
def         


        
20条回答
  •  野的像风
    2020-11-21 11:55

    From python 3.4.4, you can use multiprocessing.get_context() to obtain a context object to use multiple start methods:

    import multiprocessing as mp
    
    def foo(q, h, w):
        q.put(h + ' ' + w)
        print(h + ' ' + w)
    
    if __name__ == '__main__':
        ctx = mp.get_context('spawn')
        q = ctx.Queue()
        p = ctx.Process(target=foo, args=(q,'hello', 'world'))
        p.start()
        print(q.get())
        p.join()
    

    Or you just simply replace

    pool.map(harvester(text,case),case, 1)
    

    by:

    pool.apply_async(harvester(text,case),case, 1)
    

提交回复
热议问题