python Pool with worker Processes

后端 未结 3 2043
北恋
北恋 2020-12-04 12:33

I am trying to use a worker Pool in python using Process objects. Each worker (a Process) does some initialization (takes a non-trivial amount of time), gets passed a serie

3条回答
  •  温柔的废话
    2020-12-04 13:08

    initializer expects an arbitrary callable that does initilization e.g., it can set some globals, not a Process subclass; map accepts an arbitrary iterable:

    #!/usr/bin/env python
    import multiprocessing as mp
    
    def init(val):
        print('do some initialization here')
    
    def compute(data):
        print('Computing things!')
        return data * data
    
    def produce_data():
        yield -100
        for i in range(10):
            yield i
        yield 100
    
    if __name__=="__main__":
      p = mp.Pool(initializer=init, initargs=('arg',))
      print(p.map(compute, produce_data()))
    

提交回复
热议问题