os.system(), HTTPServer, and finishing HTTP requests

Donn Cave donn at u.washington.edu
Tue Aug 17 12:08:09 EDT 2004


In article <41214c28$1 at nntp.zianet.com>,
 "Erik Johnson" <ej <at> wellkeeper <dot> com> wrote:
...
> I'm not sure why those would all be open, but this little bit seems to
> resolve the problem
> (it throws OSError when it hits the first invalid file descriptor).
> 
> for fd in xrange(3, 256):
>     try:
>         os.close(fd)
>     except OSError:
>         break
> 
> If you know a better/smarter way to effect the same thing, I'd be glad to
> hear about it.

That's a variation on a common idiom.  The difference is
the break, where more commonly you'd write "pass".  If this
is happening in the context of a short-term programming effort
that has to be very efficient, it's a good idea, because the
exceptions make the close much more expensive than it would
have been in C, where we usually see this loop.  If it needs
to work for the indefinite future, I would say change the break
to pass.  You can't count on that continuous series of open
file descriptors.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list