Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions docker/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ def _stream_raw_result(self, response, chunk_size=1, decode=True):
yield out

def _read_from_socket(self, response, stream, tty=True, demux=False):
"""Consume all data from the socket, close the response and return the
data. If stream=True, then a generator is returned instead and the
caller is responsible for closing the response.
"""
socket = self._get_raw_response_socket(response)

gen = frames_iter(socket, tty)
Expand All @@ -411,8 +415,11 @@ def _read_from_socket(self, response, stream, tty=True, demux=False):
if stream:
return gen
else:
# Wait for all the frames, concatenate them, and return the result
return consume_socket_output(gen, demux=demux)
try:
# Wait for all frames, concatenate them, and return the result
return consume_socket_output(gen, demux=demux)
finally:
response.close()

def _disable_socket_timeout(self, socket):
""" Depending on the combination of python version and whether we're
Expand Down
18 changes: 14 additions & 4 deletions docker/api/exec_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .. import errors
from .. import utils
from ..types import CancellableStream


class ExecApiMixin(object):
Expand Down Expand Up @@ -127,9 +128,10 @@ def exec_start(self, exec_id, detach=False, tty=False, stream=False,
detach (bool): If true, detach from the exec command.
Default: False
tty (bool): Allocate a pseudo-TTY. Default: False
stream (bool): Stream response data. Default: False
stream (bool): Return response data progressively as an iterator
of strings, rather than a single string.
socket (bool): Return the connection socket to allow custom
read/write operations.
read/write operations. Must be closed by the caller when done.
demux (bool): Return stdout and stderr separately

Returns:
Expand Down Expand Up @@ -163,7 +165,15 @@ def exec_start(self, exec_id, detach=False, tty=False, stream=False,
stream=True
)
if detach:
return self._result(res)
try:
return self._result(res)
finally:
res.close()
if socket:
return self._get_raw_response_socket(res)
return self._read_from_socket(res, stream, tty=tty, demux=demux)

output = self._read_from_socket(res, stream, tty=tty, demux=demux)
if stream:
return CancellableStream(output, res)
else:
return output