python非阻塞读取文件

python非阻塞读取文件,第1张

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 fcntlimport oswith 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!!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5650085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存