Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90867
| Date | 2015-05-19 09:35 -0700 |
|---|---|
| From | Ethan Furman <ethan@stoneleaf.us> |
| Subject | Re: fork/exec & close file descriptors |
| References | <CANc-5UyeaWqThGFSAuGskz+S2Lrgq0ia9W9DHAie28t9GY+sww@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.143.1432053377.17265.python-list@python.org> (permalink) |
On 05/19/2015 05:59 AM, Skip Montanaro wrote:
> Due to presumed bugs in an underlying library over which I have no control, I'm considering a restart in the wee hours of the morning. The basic fork/exec dance is not a problem, but how do I discover
> all the open file descriptors in the new child process to make sure they get closed? Do I simply start at fd 3 and call os.close() on everything up to some largish fd number? Some of these file
> descriptors will have been opened by stuff well below the Python level, so I don't know them a priori.
Pandaemonium [1] (and I believe Ben Finney's daemon [2]) use something akin to the following:
def close_open_files(exclude):
max_files = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
keep = set()
for file in exclude:
if isinstance(file, baseint):
keep.add(file)
elif hasattr(file, 'fileno'):
keep.add(file.fileno())
else:
raise ValueError(
'files to not close should be either an file descriptor, '
'or a file-type object, not %r (%s)' % (type(file), file))
for fd in range(max_files, -1, -1):
if fd in keep:
continue
try:
os.close(fd)
except OSError:
exc = sys.exc_info()[1]
if exc.errno == errno.EBADF:
continue
raise
So, yeah, basically a brute-force method.
--
~Ethan~
[1] https://pypi.python.org/pypi/pandaemonium
[2] https://pypi.python.org/pypi/python-daemon
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: fork/exec & close file descriptors Ethan Furman <ethan@stoneleaf.us> - 2015-05-19 09:35 -0700
csiph-web