python Non-block read file -
i want read file non-block mode. did below
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!!"
but value flag
still represents 0. why..? think should changed according os.o_nonblock
and of course, if call fd.read(), blocked @ read().
o_nonblock
status flag, not descriptor flag. therefore use f_setfl
set file status flags, not f_setfd
, setting file descriptor flags.
also, sure pass integer file descriptor first argument fcntl.fcntl
, not python file object. use
f = open("/tmp/out", "r") fd = f.fileno() fcntl.fcntl(fd, fcntl.f_setfl, flag | os.o_nonblock)
rather than
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 open("/tmp/out", "r") 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!!"
prints
o_nonblock!!
Comments
Post a Comment