This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author johnlallen
Date 2006-12-01.20:05:34
SpamBayes Score
Marked as misclassified
Experimentation revealed that the os.popen[234]() family of methods was at least 10 times slower on AIX than it was on Solaris.  It turns out that this is because of the _run_child method in, which has this definition:

    def _run_child(self, cmd):
        if isinstance(cmd, basestring):
            cmd = ['/bin/sh', '-c', cmd]
        for i in xrange(3, MAXFD):
            except OSError:
            os.execvp(cmd[0], cmd)

MAXFD is set as follows at the top of

    MAXFD = os.sysconf('SC_OPEN_MAX')
except (AttributeError, ValueError):
    MAXFD = 256

On AIX, SC_OPEN_MAX is 32767, whereas on Solaris, it is not defined, and we get the default value of 256.   So, on AIX the python code for loop is being used to close 32763 file descriptors, but only 253 on Solaris.  The slowness of this python loop is the source of the problem.

Several solutions are possible.  AIX provides a much faster way to close all file descriptors above a given one using the fcntl F_CLOSEM option.  In this case, it would be: fcntl(3, F_CLOSEM, 0).  Other OSes, like Solaris and BSD, have the closefrom() function instead.  I think ideally, we would want to have an os.closefrom() method defined in posixmodule.c, and always available on every OS, and have call that instead of doing the loop.  The closefrom() function would be defined something like this:

Close all file descriptors greater than or equal to fd (for low level IO).");

static PyObject *
posix_closefrom(PyObject *self, PyObject *args)
        int fd, maxfd, res;
        if (!PyArg_ParseTuple(args, "i:closefrom", &fd))
                return NULL;

        res = closefrom(fd);
#ifdef F_CLOSEM
        res = fcntl(3, F_CLOSEM, 0)

#if defined( HAVE_SYSCONF ) && defined( _SC_OPEN_MAX )
#  define PY_OPEN_MAX sysconf(_SC_OPEN_MAX)
#  ifdef FOPEN_MAX
#  else
#    ifdef OPEN_MAX
#      define PY_OPEN_MAX OPEN_MAX
#    else
#      ifdef _NFILE
#        define PY_OPEN_MAX _NFILE
#      else
#        define PY_OPEN_MAX 256
#      endif
#    endif
#  endif

        maxfd = PY_OPEN_MAX;

        while (fd < maxfd) {
          res = close(fd);


        if (res < 0)
                return posix_error();
        return Py_None;

While this is probably (close to) the ideal solution (since it would benefit all OSes by avoiding the close loop if possible or if not, moving it to C code), adding os.closefrom() probably needs to be discussed further before being accepted by the Python community.

Instead, I will provide a simpler patch that only benefits AIX.   It adds the F_CLOSEM attribute to the fcntl class in fcntlmodule.c, if defined, and modifies to check for it and use it if possible, instead of doing the close() loop.   See the attached patch (against the 2.5 source).  I don't believe that any documentation has to change.

John Allen
Date User Action Args
2007-08-23 15:55:29adminlinkissue1607087 messages
2007-08-23 15:55:29admincreate