From 2849147f8aeb990c4f55a168e3d32ec2167ca093 Mon Sep 17 00:00:00 2001 From: benoitc Date: Sun, 19 Oct 2014 10:56:29 +0200 Subject: [PATCH] allows to enable/disable the use of sendfile() fix #856 --- gunicorn/config.py | 10 ++++++++++ gunicorn/http/wsgi.py | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/gunicorn/config.py b/gunicorn/config.py index 2d73d111..d898ce83 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -816,6 +816,16 @@ class PreloadApp(Setting): restarting workers. """ +class Sendfile(Setting): + name = "sendfile" + section = "Server Mechanics" + cli = ["--sendfile"] + validator = validate_bool + action = "store_true" + default = True + desc = """\ + Enables or disables the use of sendfile(). + """ class Chdir(Setting): name = "chdir" diff --git a/gunicorn/http/wsgi.py b/gunicorn/http/wsgi.py index ca18ce43..d9dbf442 100644 --- a/gunicorn/http/wsgi.py +++ b/gunicorn/http/wsgi.py @@ -344,6 +344,9 @@ class Response(object): self.sent += tosend util.write(self.sock, arg, self.chunked) + def can_sendfile(self): + return (self.cfg.sendfile and (sendfile is not None)) + def sendfile_all(self, fileno, sockno, offset, nbytes): # Send file in at most 1GB blocks as some operating # systems can have problems with sending files in blocks @@ -380,7 +383,7 @@ class Response(object): util.write(self.sock, data, self.chunked) def write_file(self, respiter): - if sendfile is not None and util.is_fileobject(respiter.filelike): + if can_sendfile() and util.is_fileobject(respiter.filelike): # sometimes the fileno isn't a callable if six.callable(respiter.filelike.fileno): fileno = respiter.filelike.fileno()