Skip to content

Commit 2834235

Browse files
committed
Merge pull request #629 from jashandeep-sohi/env-nosendfile
Allow disabling sendfile w/ env 'AIOHTTP_NOSENDFILE=1'
2 parents 483dd39 + 82451b6 commit 2834235

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

aiohttp/web_urldispatcher.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ def __init__(self, name, prefix, directory, *,
163163
raise ValueError(
164164
"No directory exists at '{}'".format(self._directory))
165165

166+
if os.environ.get("AIOHTTP_NOSENDFILE") == "1":
167+
self._sendfile = self._sendfile_fallback
168+
166169
def match(self, path):
167170
if not path.startswith(self._prefix):
168171
return None

docs/web_reference.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,16 +1125,31 @@ Router is any object that implements :class:`AbstractRouter` interface.
11251125
.. method:: add_static(prefix, path, *, name=None, expect_handler=None, \
11261126
chunk_size=256*1024, response_factory=StreamResponse)
11271127

1128-
Adds router for returning static files.
1128+
Adds a router and a handler for returning static files.
11291129

1130-
Useful for handling static content like images, javascript and css files.
1130+
Useful for serving static content like images, javascript and css files.
1131+
1132+
On platforms that support it, the handler will transfer files more
1133+
efficiently using the ``sendfile`` system call.
1134+
1135+
In some situations it might be necessary to avoid using the ``sendfile``
1136+
system call even if the platform supports it. This can be accomplished by
1137+
by setting environment variable ``AIOHTTP_NOSENDFILE=1``.
11311138

11321139
.. warning::
11331140

11341141
Use :meth:`add_static` for development only. In production,
11351142
static content should be processed by web servers like *nginx*
11361143
or *apache*.
11371144

1145+
.. versionchanged:: 0.18.0
1146+
Transfer files using the ``sendfile`` system call on supported
1147+
platforms.
1148+
1149+
.. versionchanged:: 0.19.0
1150+
Disable ``sendfile`` by setting environment variable
1151+
``AIOHTTP_NOSENDFILE=1``
1152+
11381153
:param str prefix: URL path prefix for handled static files
11391154

11401155
:param str path: path to the folder in file system that contains

tests/test_web_functional.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,13 @@ def go(dirname, filename):
10191019

10201020
self.loop.run_until_complete(go(here, filename))
10211021

1022+
def test_env_nosendfile(self):
1023+
directory = os.path.dirname(__file__)
1024+
1025+
with mock.patch.dict(os.environ, {'AIOHTTP_NOSENDFILE': '1'}):
1026+
route = web.StaticRoute(None, "/", directory)
1027+
self.assertEqual(route._sendfile, route._sendfile_fallback)
1028+
10221029

10231030
class TestStaticFileSendfileFallback(StaticFileMixin,
10241031
unittest.TestCase):

0 commit comments

Comments
 (0)