简体   繁体   English

具有大对象的Python多处理管道将挂起

[英]Python multiprocessing Pipe with large object will hang

I have a simple code snippet below that demonstrates the problem. 下面我有一个简单的代码片段演示了此问题。

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))

The above works fine and is fast enough for my purposes, no problem there. 上面的工作正常,并且足够快达到我的目的,在那里没有问题。 But if I make the size to 5000, it simply hangs indefinitely: 但是,如果我做了大小5000,它只是无限期挂起:

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))

Is there a size limit for Pipe, or is this a non reproducible problem? 是否有管道大小限制,或者这是一个非重复性的问题? How about if you make the size even bigger? 怎么样,如果你做的尺寸更大? And if there is a size limit, what is the best way to avoid this problem and send over the large dictionary through Pipe? 如果有一个大小的限制,什么是为了避免这个问题,并通过管道传送过来的大字典的最佳方式? Thanks in advance! 提前致谢!

This problem occurs is that Pipe.send() is a blocking call and it waits to be received. 发生此问题是因为Pipe.send()是阻塞调用,并且它等待接收。 Read more here . 在这里阅读更多。 To make it work, you can create process like in following code: 要使其工作,您可以像下面的代码那样创建过程:

#!/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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM