diff options
| author | Raymond Hettinger <python@rcn.com> | 2014-05-18 19:04:01 (GMT) |
|---|---|---|
| committer | Raymond Hettinger <python@rcn.com> | 2014-05-18 19:04:01 (GMT) |
| commit | 1c1d1e2baec5b44f40892d1691f84e7d33296b62 (patch) | |
| tree | 98e61454737975b0dd06761d9b9304e8bbff78fa | |
| parent | f6cc6e6b05dd0a416287fde92cf35492d99bc6db (diff) | |
| download | cpython-1c1d1e2baec5b44f40892d1691f84e7d33296b62.zip cpython-1c1d1e2baec5b44f40892d1691f84e7d33296b62.tar.gz cpython-1c1d1e2baec5b44f40892d1691f84e7d33296b62.tar.bz2 | |
Don't grow strings by concatenation. Use ''.join() instead.
| -rw-r--r-- | Doc/howto/sockets.rst | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst index e0083ed..0a7fcf5 100644 --- a/Doc/howto/sockets.rst +++ b/Doc/howto/sockets.rst @@ -207,13 +207,15 @@ length message:: totalsent = totalsent + sent def myreceive(self): - msg = '' - while len(msg) < MSGLEN: - chunk = self.sock.recv(MSGLEN-len(msg)) + chunks = [] + bytes_recd = 0 + while bytes_recd < MSGLEN: + chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048)) if chunk == '': raise RuntimeError("socket connection broken") - msg = msg + chunk - return msg + chucks.append(chunk) + bytes_recd = bytes_recd + len(chunk) + return ''.join(chunks) The sending code here is usable for almost any messaging scheme - in Python you send strings, and you can use ``len()`` to determine its length (even if it has |
