How do you pass a Queue reference to a function managed by pool.map_async()?

后端 未结 2 627
耶瑟儿~
耶瑟儿~ 2020-11-28 11:34

I want a long-running process to return its progress over a Queue (or something similar) which I will feed to a progress bar dialog. I also need the result when the process

2条回答
  •  遥遥无期
    2020-11-28 12:08

    Making q global works...:

    import multiprocessing, time
    
    q = multiprocessing.Queue()
    
    def task(count):
        for i in xrange(count):
            q.put("%d mississippi" % i)
        return "Done"
    
    def main():
        pool = multiprocessing.Pool()
        result = pool.map_async(task, range(10))
        time.sleep(1)
        while not q.empty():
            print q.get()
        print result.get()
    
    if __name__ == "__main__":
        main()
    

    If you need multiple queues, e.g. to avoid mixing up the progress of the various pool processes, a global list of queues should work (of course, each process will then need to know what index in the list to use, but that's OK to pass as an argument;-).

提交回复
热议问题