我想读取一个非块模式的文件。 所以我喜欢下面
import fcntl
import os
fd = open("./filename", "r")
flag = fcntl.fcntl(fd.fileno(), fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, flag | os.O_NONBLOCK)
flag = fcntl.fcntl(fd, fcntl.F_GETFD)
if flag & os.O_NONBLOCK:
print "O_NONBLOCK!!"
但是值flag
仍然表示0。
为什么?我想我应该根据os.O_NONBLOCK
来改变
当然,如果我调用fd.read(),它在read()被阻塞。
O_NONBLOCK
是状态标志,而不是描述符标志。因此,使用F_SETFL
来set File status flags,而不是F_SETFD
,这是用于setting File descriptor flags。另外,请确保将整数文件描述符作为第一个参数传递给
fcntl.fcntl
,而不是Python文件对象。因此使用而不是
印刷品
相关问题 更多 >
编程相关推荐