python Pool with worker Processes

后端 未结 3 2048
北恋
北恋 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

    I would suggest that you use a Queue for this.

    class Worker(Process):
        def __init__(self, queue):
            super(Worker, self).__init__()
            self.queue = queue
    
        def run(self):
            print('Worker started')
            # do some initialization here
    
            print('Computing things!')
            for data in iter(self.queue.get, None):
                # Use data
    

    Now you can start a pile of these, all getting work from a single queue

    request_queue = Queue()
    for i in range(4):
        Worker(request_queue).start()
    for data in the_real_source:
        request_queue.put(data)
    # Sentinel objects to allow clean shutdown: 1 per worker.
    for i in range(4):
        request_queue.put(None) 
    

    That kind of thing should allow you to amortize the expensive startup cost across multiple workers.

提交回复
热议问题