Message192739
In case someone is wondering if the approach really reduces the amount of syscalls: yes, it does. readdir() doesn't do a syscall for each entry. On Linux it uses the internal syscall getdents() to fill a buffer of directory entry structs. http://man7.org/linux/man-pages/man2/getdents.2.html
On my system os.listdir() does four syscalls:
$ strace python -c "import os; os.listdir('/home/heimes')"
openat(AT_FDCWD, "/home/heimes", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
getdents(3, /* 381 entries */, 32768) = 12880
getdents(3, /* 0 entries */, 32768) = 0
close(3)
On Linux you can also use /proc/self/fd instead of /proc/YOURPID/fd.
Other operating systems have different APIs to get a list of open FDs. AFAK /dev/fd is static on FreeBSD and Mac OS X:
FreeBSD:
http://www.manualpages.de/FreeBSD/FreeBSD-7.4-RELEASE/man3/kinfo_getfile.3.html
Darwin / Mac OS X:
proc_pidinfo() |
|
Date |
User |
Action |
Args |
2013-07-09 11:31:00 | christian.heimes | set | recipients:
+ christian.heimes, georg.brandl, gregory.p.smith, ronaldoussoren, ferringb, rosslagerwall |
2013-07-09 11:31:00 | christian.heimes | set | messageid: <1373369460.33.0.363639251888.issue13788@psf.upfronthosting.co.za> |
2013-07-09 11:31:00 | christian.heimes | link | issue13788 messages |
2013-07-09 11:30:59 | christian.heimes | create | |
|