[Twisted-Python] reactor.spawnProcess() and file descriptors
Carlos Valiente
superdupont at gmail.com
Mon Jun 28 06:21:09 MDT 2010
On Mon, Jun 28, 2010 at 13:04, Itamar Turner-Trauring
<itamar at itamarst.org> wrote:
> On Mon, 2010-06-28 at 12:52 +0100, Carlos Valiente wrote:
>> Hi! Is there any way of telling reactor.spawnProcess() not to close
>> all open file descriptors?
>
> My Unix knowledge is weak, but - couldn't you pass in duplicate fds, so
> that when they are closed it wouldn't affect the originals?
Thanks for your reply, Itamar. I wanted to skip the calls to close()
-- 1024 of them, in my case -- because, according to strace(1), that
takes about 100 ms (and I'd like to save those milliseconds). From
reading twisted.internet.process.Process._setupChild(), it seems that
passing a dupped file descriptor would trigger at least another call
to dup():
for child in childlist:
target = fdmap[child]
if target == child:
# fd is already in place
if debug: print >>errfd, "%d already in place" % target
fdesc._unsetCloseOnExec(child)
else:
if child in fdmap.values():
# we can't replace child-fd yet, as some other mapping
# still needs the fd it wants to target. We must preserve
# that old fd by duping it to a new home.
newtarget = os.dup(child) # give it a safe home
if debug: print >>errfd, "os.dup(%d) -> %d" % (child,
newtarget)
os.close(child) # close the original
for c, p in fdmap.items():
if p == child:
fdmap[c] = newtarget # update all pointers
# now it should be available
if debug: print >>errfd, "os.dup2(%d,%d)" % (target, child)
os.dup2(target, child)
More information about the Twisted-Python
mailing list