@@ -1126,7 +1126,7 @@ def start(self, container, *args, **kwargs):
1126
1126
self ._raise_for_status (res )
1127
1127
1128
1128
@utils .check_resource ('container' )
1129
- def stats (self , container , decode = None , stream = True ):
1129
+ def stats (self , container , decode = None , stream = True , one_shot = None ):
1130
1130
"""
1131
1131
Stream statistics for a specific container. Similar to the
1132
1132
``docker stats`` command.
@@ -1138,23 +1138,39 @@ def stats(self, container, decode=None, stream=True):
1138
1138
False by default.
1139
1139
stream (bool): If set to false, only the current stats will be
1140
1140
returned instead of a stream. True by default.
1141
+ one_shot (bool): If set to true, Only get a single stat instead of
1142
+ waiting for 2 cycles. Must be used with stream=false. False by
1143
+ default.
1141
1144
1142
1145
Raises:
1143
1146
:py:class:`docker.errors.APIError`
1144
1147
If the server returns an error.
1145
1148
1146
1149
"""
1147
1150
url = self ._url ("/containers/{0}/stats" , container )
1151
+ params = {
1152
+ 'stream' : stream
1153
+ }
1154
+ if one_shot is not None :
1155
+ if utils .version_lt (self ._version , '1.41' ):
1156
+ raise errors .InvalidVersion (
1157
+ 'one_shot is not supported for API version < 1.41'
1158
+ )
1159
+ params ['one-shot' ] = one_shot
1148
1160
if stream :
1149
- return self ._stream_helper (self ._get (url , stream = True ),
1161
+ if one_shot :
1162
+ raise errors .InvalidArgument (
1163
+ 'one_shot is only available in conjunction with '
1164
+ 'stream=False'
1165
+ )
1166
+ return self ._stream_helper (self ._get (url , params = params ),
1150
1167
decode = decode )
1151
1168
else :
1152
1169
if decode :
1153
1170
raise errors .InvalidArgument (
1154
1171
"decode is only available in conjunction with stream=True"
1155
1172
)
1156
- return self ._result (self ._get (url , params = {'stream' : False }),
1157
- json = True )
1173
+ return self ._result (self ._get (url , params = params ), json = True )
1158
1174
1159
1175
@utils .check_resource ('container' )
1160
1176
def stop (self , container , timeout = None ):
0 commit comments