Skip to content

Commit ced17c6

Browse files
committed
stream/ws_client: Use StringIO for WSClient._all
bytes() += bytes() copies both buffers into a new one, causing exponential cost and gradual slow-down. Replacing with StringIO improves that
1 parent a2d1024 commit ced17c6

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

stream/ws_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import yaml
2525

2626
from six.moves.urllib.parse import urlencode, quote_plus, urlparse, urlunparse
27+
from six import StringIO
2728

2829
from websocket import WebSocket, ABNF, enableTrace
2930

@@ -47,7 +48,7 @@ def __init__(self, configuration, url, headers):
4748
header = []
4849
self._connected = False
4950
self._channels = {}
50-
self._all = ""
51+
self._all = StringIO()
5152

5253
# We just need to pass the Authorization, ignore all the other
5354
# http headers we get from the generated code
@@ -157,8 +158,8 @@ def read_all(self):
157158
TODO: Maybe we can process this and return a more meaningful map with
158159
channels mapped for each input.
159160
"""
160-
out = self._all
161-
self._all = ""
161+
out = self._all.getvalue()
162+
self._all = self._all.__class__()
162163
self._channels = {}
163164
return out
164165

@@ -195,7 +196,7 @@ def update(self, timeout=0):
195196
if channel in [STDOUT_CHANNEL, STDERR_CHANNEL]:
196197
# keeping all messages in the order they received
197198
# for non-blocking call.
198-
self._all += data
199+
self._all.write(data)
199200
if channel not in self._channels:
200201
self._channels[channel] = data
201202
else:

0 commit comments

Comments
 (0)