python multiprocessing - process hangs on join for large queue

前端 未结 4 1612
攒了一身酷
攒了一身酷 2020-12-14 07:10

I\'m running python 2.7.3 and I noticed the following strange behavior. Consider this minimal example:

from multiprocessing import Process, Queue

def foo(qi         


        
4条回答
  •  生来不讨喜
    2020-12-14 07:47

    I had the same problem on python3 when tried to put strings into a queue of total size about 5000 cahrs.

    In my project there was a host process that sets up a queue and starts subprocess, then joins. Afrer join host process reads form the queue. When subprocess producess too much data, host hungs on join. I fixed this using the following function to wait for subprocess in the host process:

    from multiprocessing import Process, Queue
    from queue import Empty
    
    def yield_from_process(q: Queue, p: Process):
        while p.is_alive():
            p.join(timeout=1)
            while True:
                try:
                    yield q.get(block=False)
                except Empty:
                    break
    

    I read from queue as soon as it fills so it never gets very large

提交回复
热议问题