Finding count of currently open file descriptors.
Andreas Kostyrka
andreas at kostyrka.priv.at
Thu Aug 1 04:37:17 EDT 2002
Am Die, 2002-07-30 um 16.31 schrieb Tim McNerney:
> > In a Python debug build, you can find out all file objects, but there
> > may be file descriptors not controlled by Python file objects (e.g. if
> > opened by os.open).
>
> This is more what I'm looking for -- some generic method which is within
> Python itself. How would I go about doing this within the debug build?
Well, an "easy" way would be:
for i in xrange(64*1024):
try:
do something with fd
count=count+1
except IOError:
pass
For do something, you can use fstat:
>>> def countfd():
... count=0
... for i in xrange(64*1024):
... try:
... os.fstat(i)
... count+=1
... except os.error:
... pass
... return count
...
>>> countfd()
3
>>> f=open("/dev/null")
>>> countfd()
4
Only problem, scanning 64K descriptors take some seconds or so here.
Perhaps in your situation you could limit somehow the number that must
be scanned.
Even better, use isatty, on my Linux (Duron700 SonyFX201) system it's
tenfold faster:
isatty: 0.308787202835
fstat : 3.40730090141
I'l email you privatly the module with the countfd function.
Andreas
Andreas
More information about the Python-list
mailing list