Filling a queue and managing multiprocessing in python

前端 未结 2 1899
暗喜
暗喜 2020-11-28 10:15

I\'m having this problem in python:

  • I have a queue of URLs that I need to check from time to time
  • if the queue is filled up, I need to process each it
2条回答
  •  余生分开走
    2020-11-28 10:26

    Added some code (submitting "None" to the queue) to nicely shut down the worker threads, and added code to close and join the_queue and the_pool:

    import multiprocessing
    import os
    import time
    
    NUM_PROCESSES = 20
    NUM_QUEUE_ITEMS = 20  # so really 40, because hello and world are processed separately
    
    
    def worker_main(queue):
        print(os.getpid(),"working")
        while True:
            item = queue.get(block=True) #block=True means make a blocking call to wait for items in queue
            if item is None:
                break
    
            print(os.getpid(), "got", item)
            time.sleep(1) # simulate a "long" operation
    
    
    def main():
        the_queue = multiprocessing.Queue()
        the_pool = multiprocessing.Pool(NUM_PROCESSES, worker_main,(the_queue,))
                
        for i in range(NUM_QUEUE_ITEMS):
            the_queue.put("hello")
            the_queue.put("world")
        
        for i in range(NUM_PROCESSES):
            the_queue.put(None)
    
        # prevent adding anything more to the queue and wait for queue to empty
        the_queue.close()
        the_queue.join_thread()
    
        # prevent adding anything more to the process pool and wait for all processes to finish
        the_pool.close()
        the_pool.join()
    
    if __name__ == '__main__':
        main()
    

提交回复
热议问题