문제

I have a program and somewhere it leaks the file descriptors, so I want to add a monitor thread to detect the process open fds, if the fd is a "PIPE" , I will close it. I could use lsof -p or cat /proc/pid/fd to get the process number, and I could also use the grep to filtr out the TYPE, but I wonder to know if there is built-in function in python which could used to get the type of file descriptor? something like this:

def getFdType(fd)

도움이 되었습니까?

해결책

You could use readlink on the descriptor.

If the result is not a file, then its a pipe. You could also further run stat:

On some Unix systems (such as Linux), the following attributes may also be available:

st_blocks - number of 512-byte blocks allocated for file

st_blksize - filesystem blocksize for efficient file system I/O

st_rdev - type of device if an inode device

st_flags - user defined flags for file

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top