parameters: (keep_fds)
docstring: Close all the file descriptors except those in keep_fds.
docstring_summary: Close all the file descriptors except those in keep_fds.
docstring_tokens: Close all the file descriptors except those in keep_fds .
function: def close_fds(keep_fds):  # pragma: no cover
      """Close all the file descriptors except those in keep_fds."""
  
      # Make sure to keep stdout and stderr open for logging purpose
      keep_fds = set(keep_fds).union([1, 2])
  
      # We try to retrieve all the open fds
      try:
          open_fds = set(int(fd) for fd in os.listdir('/proc/self/fd'))
      except FileNotFoundError:
          import resource
          max_nfds = resource.getrlimit(resource.RLIMIT_NOFILE)[0]
          open_fds = set(fd for fd in range(3, max_nfds))
          open_fds.add(0)
  
      for i in open_fds - keep_fds:
          try:
              os.close(i)
          except OSError:
              pass
function_tokens: def close_fds ( keep_fds ) : # pragma: no cover # Make sure to keep stdout and stderr open for logging purpose keep_fds = set ( keep_fds ) . union ( [ 1 , 2 ] ) # We try to retrieve all the open fds try : open_fds = set ( int ( fd ) for fd in os . listdir ( '/proc/self/fd' ) ) except FileNotFoundError : import resource max_nfds = resource . getrlimit ( resource . RLIMIT_NOFILE ) [ 0 ] open_fds = set ( fd for fd in range ( 3 , max_nfds ) ) open_fds . add ( 0 ) for i in open_fds - keep_fds : try : os . close ( i ) except OSError : pass