Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dup a socket for sendfile usage #964

Merged
merged 1 commit into from
Jul 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions aiohttp/file_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _sendfile_cb(self, fut, out_fd, in_fd, offset,
@asyncio.coroutine
def _sendfile_system(self, req, resp, fobj, count):
"""
Write `count` bytes of `fobj` to `resp` starting from `offset` using
Write `count` bytes of `fobj` to `resp` using
the ``sendfile`` system call.

`req` should be a :obj:`aiohttp.web.Request` instance.
Expand All @@ -48,8 +48,6 @@ def _sendfile_system(self, req, resp, fobj, count):

`fobj` should be an open file object.

`offset` should be an integer >= 0.

`count` should be an integer > 0.
"""
transport = req.transport
Expand All @@ -61,13 +59,18 @@ def _sendfile_system(self, req, resp, fobj, count):
yield from resp.drain()

loop = req.app.loop
out_fd = transport.get_extra_info("socket").fileno()
# See https://github.com/KeepSafe/aiohttp/issues/958 for details
out_socket = transport.get_extra_info("socket").dup()
out_fd = out_socket.fileno()
in_fd = fobj.fileno()
fut = create_future(loop)

self._sendfile_cb(fut, out_fd, in_fd, 0, count, loop, False)
try:
self._sendfile_cb(fut, out_fd, in_fd, 0, count, loop, False)

yield from fut
yield from fut
finally:
out_socket.close()

@asyncio.coroutine
def _sendfile_fallback(self, req, resp, fobj, count):
Expand Down