我想以非阻塞模式读取文件,所以我按照以下方式进行了操作。
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()
处被阻塞。