Вопрос

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