我正在寻找有关Python中队列实现的更多见解,而不仅仅是文档中提供的内容。
根据我所理解的(如果我错了,请原谅我的无知):
queue.Queue()
:通过基本的内存数组实现,因此不能在多个进程之间共享,但可以在线程之间共享。
multiprocessing.Queue()
:通过管道(man 2 pipes
)实现,具有大小限制(相当小:man 7 pipe
在Linux上说,未调整为65536字节):
自Linux 2.6.35以来,默认管道容量为65536字节,但可以使用
fcntl(2)
的F_GETPIPE_SZ
和F_SETPIPE_SZ
操作进行查询和设置容量。
但是,在Python中,无论何时尝试将数据写入管道中,如果其大小大于65536字节,则会正常工作 - 这种情况下可能会将内存填满:
import multiprocessing
from time import sleep
def big():
result = ""
for i in range(1,70000):
result += ","+str(i)
return result # 408888 bytes string
def writequeue(q):
while True:
q.put(big())
sleep(0.1)
if __name__ == '__main__':
q = multiprocessing.Queue()
p = multiprocessing.Process(target=writequeue, args=(q,))
p.start()
while True:
sleep(1) # No pipe consumption, we just want to flood the pipe
所以这是我的问题:
Python是否调整了管道限制?如果是,增加了多少?欢迎提供Python源代码。
Python的管道通信是否与其他非Python进程兼容?如果是,欢迎提供工作示例(最好是JS)和资源链接。