python - 如何在 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')
解决方案
推荐阅读
- ios - Swift IPv4 和 IPv6 地址蜂窝数据
- java - Webview 卡在加载网站或 cloudfare 检查
- dart - flutter - bloc - 我如何在我的 Ui 中使用 FutureBuilder 来正确实现 Bloc 架构
- angular - 无法在 angular-cli 项目中使用 npm 包含 Bootstrap
- c# - Botframework v4:从没有stackoverflow异常的子对话框调用父对话框
- serialization - Kotlin - 将 JSON 数组序列化为多个类
- c# - 如何获取 ViewResult 的索引?
- swift - 更新 tableview 和 coredata
- reactjs - 如何从对象访问数据并将其放入组件中?
- javascript - 如何访问另一个网页中的一个 php 变量