您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python非阻塞读取文件

python非阻塞读取文件

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!!
python 2022/1/1 18:40:03 有255人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶