代码之家  ›  专栏  ›  技术社区  ›  StatsNoob

大对象的python多处理管道将挂起

  •  0
  • StatsNoob  · 技术社区  · 6 年前

    from multiprocessing import Pipe
    import time
    
    recv_end, send_end = Pipe(duplex=False)
    d = {'word'+str(elem): elem for elem in range(3000)}
    
    start_time = time.time()
    send_end.send(d)
    print('--- %s seconds ---' % (time.time()-start_time))
    

    from multiprocessing import Pipe
    import time
    
    recv_end, send_end = Pipe(duplex=False)
    d = {'word'+str(elem): elem for elem in range(5000)}  # changed to 5000
    
    start_time = time.time()
    send_end.send(d)
    print('--- %s seconds ---' % (time.time()-start_time))
    

    管道是否有尺寸限制,或者这是一个不可重复的问题?如果你把尺寸再大一点怎么样?如果有大小限制,最好的方法是什么来避免这个问题,并通过管道发送大字典?事先谢谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   RedEyed    6 年前

    出现的问题是 Pipe.send() here .

    #!/usr/bin/env python
    from multiprocessing import Pipe, Process
    import time
    import sys
    
    
    def foo(conn):
        d = {'word'+str(elem): elem for elem in range(5000)}  # changed to 5000
        conn.send(d)
        conn.close()
    
    
    recv_end, send_end = Pipe(duplex=False)
    p = Process(target=foo, args=(send_end, ))
    p.start()
    
    start_time = time.time()
    recv_end.recv()  # Try to comment and you will see that it waits for being received
    p.join()
    print('--- %s seconds ---' % (time.time()-start_time))