O_NONBLOCK
是状态标志,而不是描述符标志。因此,请使用F_SETFL
来设置文件状态标志,而不是F_SETFD
用于设置文件描述符标志。
另外,请确保将整数文件描述符作为第一个参数传递给fcntl.fcntl
,而不是Python文件对象。因此使用
f = open("/tmp/out", "r")
fd = f.fileno()
fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK)
而不是
fd = open("/tmp/out", "r")
...
fcntl.fcntl(fd, fcntl.F_SETFD, flag | os.O_NONBLOCK)
flag = fcntl.fcntl(fd, fcntl.F_GETFD)
import fcntl
import os
with open("/tmp/out", "r") as f:
fd = f.fileno()
flag = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flag | os.O_NONBLOCK)
flag = fcntl.fcntl(fd, fcntl.F_GETFL)
if flag & os.O_NONBLOCK:
print "O_NONBLOCK!!"
版画
O_NONBLOCK!!