move close_on_exec in util

This commit is contained in:
Benoit Chesneau 2010-01-21 12:56:16 +01:00
parent 2f959f9251
commit 354a91ad35
3 changed files with 28 additions and 21 deletions

View File

@ -15,8 +15,6 @@ import time
from gunicorn.worker import Worker
class Arbiter(object):
LISTENER = None

View File

@ -4,6 +4,8 @@
# See the NOTICE for more information.
import errno
import fcntl
import os
import select
import socket
import time
@ -20,6 +22,11 @@ weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
monthname = [None,
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
def close_on_exec(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
def close(sock):
""" socket.close() doesn't *really* close if
@ -36,7 +43,7 @@ def close(sock):
del sock
def read_partial(sock, length):
def read_partial(sock, length):
while True:
try:
ret = select.select([sock.fileno()], [], [], 0)

View File

@ -5,7 +5,6 @@
import errno
import fcntl
import logging
import os
import select
@ -35,25 +34,20 @@ class Worker(object):
self.tmpname = tmpname
# prevent inherientence
self.close_on_exec(socket)
self.close_on_exec(fd)
# Set blocking to 0 back since we
# prevented inheritence of the socket
# the socket.
socket.setblocking(0)
self.socket = socket
self.address = socket.getsockname()
util.close_on_exec(self.socket)
self.socket.setblocking(0)
util.close_on_exec(fd)
self.address = self.socket.getsockname()
self.app = app
self.alive = True
self.log = logging.getLogger(__name__)
def close_on_exec(self, fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC
fcntl.fcntl(fd, fcntl.F_SETFL, flags)
def init_signals(self):
map(lambda s: signal.signal(s, signal.SIG_DFL), self.SIGNALS)
@ -97,9 +91,17 @@ class Worker(object):
self._fchmod(spinner)
nr += 1
except socket.error, e:
if e[0] in [errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK]:
if e[0] in (errno.EAGAIN, errno.EWOULDBLOCK,
errno.ECONNABORTED):
break # Uh oh!
elif e[0] in (errno.EMFILE, errno.ENOBUFS, errno.ENFILE,
errno.ENOMEM):
"""
linux gave EMFILE when a process is not allowed to
allocate more fs or ENOMEM when there is not enough
memory to allocate. BSD return ENOBUFS.
"""
log.info("Could not accept new connection (%s)" % str(e))
raise
if nr == 0: break
@ -122,7 +124,7 @@ class Worker(object):
self._fchmod(spinner)
def handle(self, client, addr):
self.close_on_exec(client)
#util.close_on_exec(client)
try:
req = http.HttpRequest(client, addr, self.address)
response = self.app(req.read(), req.start_response)