首页 > 解决方案 > 如何在 Python / Windows 中绕过管道缓冲区大小

问题描述

我正在使用的二进制数据大约有 80Kb,我需要将其写入管道,然后在另一个函数中读取它。问题是缓冲区大小仅限于 4096 字节,因此它不会写入(而是冻结)。

如何使用管道读写这 80Kb 的数据?

file_path = os.path.join(temp_dir, 'abc.png')
with open(file_path, 'rb', buffering=0) as f:
    content = f.read()

content_length = len(bytearray(content))
print(f'content_length: {content_length}')

# if I enable the below, it's written and read
# -> only for limiting and testing the max size

# content = str(content)[: 4096] # max length / buffer (io) 4096
# content = content.encode('ascii')

r, w = os.pipe()
os.write(w, content)
print('Writing successfull')


def open_buffer(buffer, content_length):
    f = open(buffer, 'rb', buffering=0)
    content = f.read(content_length)
    #print(content)
    f.close()
    return content

open_buffer(r, content_length)
print('Success')

标签: pythonpipebuffer

解决方案


推荐阅读