转载自Python:linux 环境下如何修改PIPE bufer size
当 PIPE 的写入方写的太快,但是读取方来不及读取的时候,就会把 buffer 给写满从而出现 resource temporarily unavailable 错误。在 linux 下可以通过 fcntl.fcntl(fd, fcntl.F_SETPIPE_SZ, size) 来设置 PIPE 的 buffer 大小。
相关 Python 代码如下:
1 | import fcntl |
一般可以通过 read_fd, write_fd = os.pipe() 来生成 pipe, 此时上面的 fd 对应 read_fd 和 write_fd 。
- 捕获
IOError异常是因为有的 linux 内核版本不支持修改 PIPE buffer 大小。 - python 的 fcntl 没有
F_SETPIPE_SZ属性,所以我们定义了这个属性,它的值来自 fcntl.h 。