Ticket #4522: 0001-Speed-up-reactor.spawnProcess.patch

File 0001-Speed-up-reactor.spawnProcess.patch, 2.1 KB (added by Carlos Valiente, 12 years ago)

Generated from the Git mirror

  • twisted/internet/process.py

    From 612278e2de5989c69d86c860566c22ca047fcb25 Mon Sep 17 00:00:00 2001
    From: Carlos Valiente <carlos@pepelabs.net>
    Date: Mon, 28 Jun 2010 19:06:09 +0100
    Subject: [PATCH] Speed up `reactor.spawnProcess()`.
    
    On platforms that list open file descriptors under '/proc/<pid>', this
    patch makes `reactor.spawnProcess()` close open file descriptors only,
    instead of the whole range [0, <value of ulimit -n>].
    ---
     twisted/internet/process.py |   23 +++++++++++++++--------
     1 files changed, 15 insertions(+), 8 deletions(-)
    
    diff --git a/twisted/internet/process.py b/twisted/internet/process.py
    index c5f1ac3..88040a0 100644
    a b class Process(_BaseProcess): 
    601601        This is accomplished in two steps::
    602602
    603603            1. close all file descriptors that aren't values of fdmap.  This
    604                means 0 .. maxfds.
     604               means 0 .. maxfds (or just the open fds within that range, if
     605               the platform supports '/proc/<pid>/fd').
    605606
    606607            2. for each childFD::
    607608
    class Process(_BaseProcess): 
    626627
    627628        destList = fdmap.values()
    628629        try:
    629             import resource
    630             maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1
    631             # OS-X reports 9223372036854775808. That's a lot of fds to close
    632             if maxfds > 1024:
    633                 maxfds = 1024
     630            proc_fd = "/proc/%d/fd" % os.getpid()
     631            openfds = tuple(int(fd) for fd in os.listdir(proc_fd))
    634632        except:
    635             maxfds = 256
     633            try:
     634                import resource
     635                maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1
     636                # OS-X reports 9223372036854775808. That's a lot of fds
     637                # to close
     638                if maxfds > 1024:
     639                    maxfds = 1024
     640            except:
     641                maxfds = 256
     642            openfds = xrange(maxfds)
    636643
    637         for fd in xrange(maxfds):
     644        for fd in openfds:
    638645            if fd in destList:
    639646                continue
    640647            if debug and fd == errfd.fileno():