def count_file_descriptors(): procfn = '/proc/%d/fd' % os.getpid() try: # Linux: /proc/PID/fd/* t = os.listdir(procfn) return len(t) except: pass try: # OSX: "lsof" from run_command import run_command as rc cmd = 'lsof -p %i' % os.getpid() rtn,out,err = rc(cmd) if rtn == 0: return len(out.split('\n')) except: pass return 0
def count_file_descriptors(): ''' Returns the number of file descriptors used by the current process. On Linux this uses the */proc* filesystem; on Mac OSX it uses the *lsof* command. ''' procfn = '/proc/%d/fd' % os.getpid() try: # Linux: /proc/PID/fd/* t = os.listdir(procfn) return len(t) except: pass try: # OSX: "lsof" from run_command import run_command as rc cmd = 'lsof -p %i' % os.getpid() rtn,out,err = rc(cmd) if rtn == 0: return len(out.split('\n')) except: pass return 0